|
|
|
date: Thu, 14 Aug 2008 16:14:29 -0700 (PDT),
group: microsoft.public.xsl
back
copying a node into a variable, but changing one attribute value
Please excuse the noob kind of question. I've been trying to select a
node into a variable, but I want to change the value of one of the
attributes somewhere within the node. For example, if I have the
following node:
<P id="3">
<As>
<a value="4" />
</As>
<N value="1">
<S>
<sA name="foo" />
</S>
<T value="X." />
</N>
</P>
I'm trying to copy P as-is except I want to change the value attribute
of T to a substring (e.g., substring-before(.,'.')).
So, I'm not sure how to properly construct a variable without
explicitly selecting every element (especially because there could be
any number of elements, but all I want to do is change //T[1]/@value.
I've tried something like:
<xsl:variable name="MyVar">
<xsl:copy-of select="//P" />
<xsl:attribute name="value"
<xsl:value-of select="//T/substring-before(@value,'.')" />
</xsl:attribute>
</xsl:variable>
but obviously that didn't work. This seems like a very common thing
that people would want to do, but I can't seem to find any example
code that does this.
Sorry for the noob-type question, but I appreciate any help.
--John
date: Thu, 14 Aug 2008 16:14:29 -0700 (PDT)
author: unknown
Re: copying a node into a variable, but changing one attribute value
On Aug 15, 4:26 am, Martin Honnen wrote:
> John.Frazz...@gmail.com wrote:
> > Please excuse the noob kind of question. I've been trying to select a
> > node into a variable, but I want to change the value of one of the
> > attributes somewhere within the node. For example, if I have the
> > following node:
>
> > <P id="3">
> > <As>
> > <a value="4" />
> > </As>
> > <N value="1">
> > <S>
> > <sA name="foo" />
> > </S>
> > <T value="X." />
> > </N>
> > </P>
>
> > I'm trying to copy P as-is except I want to change the value attribute
> > of T to a substring (e.g., substring-before(.,'.')).
>
> Start with the identity transformation template, then add a template for
> T/@value. The following sample then also creates a variable of the
> transformed P element as you seemed to want to do that too:
>
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xsl:template match="@* | node()">
> <xsl:copy>
> <xsl:apply-templates select="@* | node()"/>
> </xsl:copy>
> </xsl:template>
>
> <xsl:template match="T/@value">
> <xsl:attribute name="{name()}">
> <xsl:value-of select="substring-before(., '.')"/>
> </xsl:attribute>
> </xsl:template>
>
> <xsl:template match="/">
> <xsl:variable name="foo">
> <xsl:apply-templates select="P"/>
> </xsl:variable>
> <xsl:copy-of select="$foo"/>
> </xsl:template>
>
> </xsl:stylesheet>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/- Hide quoted text -
>
> - Show quoted text -
Thanks Martin, I appreciate the help.
--John
date: Fri, 15 Aug 2008 15:12:49 -0700 (PDT)
author: unknown
|
|