[Home] [By Thread] [By Date] [Recent Entries]
At 2006-06-22 16:45 +0100, Peter Riocreux wrote:
I am sure this is a hoary old one, but I could not work out the right runes to achieve it, nor the right keywords to search for to find an answer Well said. I a have a for-each loop over the W elements. Inside the for-each I want to select one X with each value of Y that exists within that W. (data copied below) So for the above example, for //W[1] I want to get the first and third or second and third X elements, for //W[2] I want all three X elements and for //W[3] I want any one of the X elements, but only one of them.
I am stuck with using XSL 1.0 That isn't a stigma to bear. I'm still being asked to teach XSLT 1.0. I hope the two approaches below are helpful. . . . . . . . . . Ken t:\ftemp>type peter.xml
<V>
<W>
<Z/>
<X Y='0'/>
<X Y='0'/>
<X Y='1'/>
<Z/>
</W>
<W>
<Z/>
<Z/>
<X Y='0'/>
<X Y='1'/>
<X Y='2'/>
</W>
<W>
<X Y='2'/>
<X Y='2'/>
<X Y='2'/>
<Z/>
<Z/>
</W>
</V>t:\ftemp>type peter.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"><xsl:output method="text"/> <xsl:template match="V">
<xsl:for-each select="W">
<xsl:value-of select="concat('/V/W[',position(),']')"/>
<xsl:text>
</xsl:text>
<xsl:variable name="Xs" select="X"/>
<xsl:for-each select="$Xs">
<xsl:if test="generate-id(.)=generate-id($Xs[@Y=current()/@Y][1])">
<xsl:value-of select="concat('got X/@Y="',@Y,'"')"/>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template></xsl:stylesheet> t:\ftemp>xslt peter.xml peter.xsl con /V/W[1] got X/@Y="0" got X/@Y="1" /V/W[2] got X/@Y="0" got X/@Y="1" got X/@Y="2" /V/W[3] got X/@Y="2" t:\ftemp>type peter2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"><xsl:output method="text"/> <xsl:key name="Xs" match="X" use="concat(generate-id(..),' ',@Y)"/> <xsl:template match="V">
<xsl:for-each select="W">
<xsl:value-of select="concat('/V/W[',position(),']')"/>
<xsl:text>
</xsl:text>
<xsl:variable name="Xs" select="X"/>
<xsl:for-each select="$Xs[generate-id(.)=
generate-id(key('Xs',concat(generate-id(..),' ',@Y))[1])]">
<xsl:value-of select="concat('got X/@Y="',@Y,'"')"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template></xsl:stylesheet> t:\ftemp>xslt peter.xml peter2.xsl con /V/W[1] got X/@Y="0" got X/@Y="1" /V/W[2] got X/@Y="0" got X/@Y="1" got X/@Y="2" /V/W[3] got X/@Y="2" t:\ftemp>
|

Cart



