Subject: Re: [xsl] Value dependency between nested attributes From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sat, 17 Oct 2009 22:35:20 -0400 |
Output format shall be same than input format (XML-->XML Transformation):
<A name="a" test1="vw" test2="cd" test3="ef" test4="gh"/> <A name="b" test1="vw" test2="cd" test3="ef" test4="gh" test7="ee"/> <A name="c" test1="vw" test2="cd" test3="ef" test4="gh"/> <A name="d" test1="xy" test2="ij" test3="kl" test4="mn" test5="op"/>
I want to give some params to the processor to change some (not all) "test" values. The values for the change depend on the value of the "name" attribute.
For example: if value of name = "a" then test2 value="$XX" and test3 value="$YY"
and if name="b" then test2 value="$VV" and test3 value="$WW"
...
I tried to customize the "Identity change template" (http://www.dpawson.co.uk/xsl/sect2/identity.html#d5687e43 ) for my needs
T:\ftemp>type karl.xml <test> <A name="a" test1="vw" test2="cd" test3="ef" test4="gh"/> <A name="b" test1="vw" test2="cd" test3="ef" test4="gh" test7="ee"/> <A name="c" test1="vw" test2="cd" test3="ef" test4="gh"/> <A name="d" test1="xy" test2="ij" test3="kl" test4="mn" test5="op"/> </test>
T:\ftemp>xslt karl.xml karl.xsl <?xml version="1.0" encoding="utf-8"?><test> <A name="a" test1="vw" test2="XXX" test3="YYY" test4="gh"/> <A name="b" test1="vw" test2="VVV" test3="WWW" test4="gh" test7="ee"/> <A name="c" test1="vw" test2="cd" test3="ef" test4="gh"/> <A name="d" test1="xy" test2="ij" test3="kl" test4="mn" test5="op"/> </test> T:\ftemp>type karl.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="XX">XXX</xsl:param> <xsl:param name="YY">YYY</xsl:param> <xsl:param name="VV">VVV</xsl:param> <xsl:param name="WW">WWW</xsl:param>
<!--method one: testing and reconstituting the element--> <xsl:template match="A[@name='a']"> <xsl:copy> <!--preserve all attributes--> <xsl:apply-templates select="@*"/> <!--overwrite the attributes needing changing--> <xsl:attribute name="test2"><xsl:value-of select="$XX"/></xsl:attribute> <xsl:attribute name="test3"><xsl:value-of select="$YY"/></xsl:attribute> <!--act on the children--> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>
<!--method two: acting only on the attributes--> <xsl:template match="A[@name='b']/@test2"> <xsl:attribute name="test2"><xsl:value-of select="$VV"/></xsl:attribute> </xsl:template> <xsl:template match="A[@name='b']/@test3"> <xsl:attribute name="test3"><xsl:value-of select="$WW"/></xsl:attribute> </xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes--> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
-- Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes in Copenhagen Denmark and Washington DC USA, October/November 2009 Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Value dependency between nest, xyz xyz | Thread | Re: [xsl] Value dependency between , xyz xyz |
[xsl] Value dependency between nest, xyz xyz | Date | Re: [xsl] Value dependency between , Syd Bauman |
Month |