Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Unsorted position in for-each Unsorted position in for-eachTo: NULL Date: 5/6/2008 9:55:00 AM
If I sort a node-set with a for-each loop, I can use:
<xsl:value-of select="position()"/>
to determine each node's position in the sorted list.
How do I retrieve the position of each node before it was sorted?
For example, here is a XML file I am starting with:
<?xml version="1.0" encoding="UTF-8"?>
<bar>
<foo>D</foo>
<foo>C</foo>
<foo>B</foo>
<foo>A</foo>
</bar>
I will sort the "foo's" and I want to create the following:
<?xml version="1.0" encoding="UTF-8"?>
<newRoot>
<foo name="A" sortedPosition="1" originalPosition="4"/>
<foo name="B" sortedPosition="2" originalPosition="3"/>
<foo name="C" sortedPosition="3" originalPosition="2"/>
<foo name="D" sortedPosition="4" originalPosition="1"/>
</newRoot>
How do I get the "originalPosition"?
Here is the XSL I am working with:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/bar">
<xsl:element name="newRoot">
<xsl:for-each select="foo">
<xsl:sort order="ascending" data-type="text"/>
<xsl:element name="foo">
<xsl:attribute name="name">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="sortedPosition">
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:attribute name="originalPosition">
<xsl:value-of
select="/some/mystery/expression()"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Sean
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
