Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Unsorted position in for-each

From: Sean Keesler <sean@-------.--->
To: 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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent