Re: [xsl] Value dependency between nested attributes

Subject: Re: [xsl] Value dependency between nested attributes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Oct 2009 22:35:20 -0400
At 2009-10-18 02:19 +0200, xyz xyz wrote:
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

Yes, this is what you need to work with.


Below I've given you two approaches, one for where the name="a" and one for where the name="b". For reasons of good style you should consistently use the same approach, but I've given you both so you can decide which is more appropriate for your final solution.

The first approach catches the <A> and then reconstitutes the <A> and the required attributes.

The second approach catches only the attributes that need changing and reconstitute only the attributes.

I hope this helps.

. . . . . . . . . . Ken

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>

</xsl:stylesheet>

T:\ftemp>

--
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