Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL Sort

Status
Not open for further replies.

tmryan

Programmer
Dec 22, 2000
73
0
0
US
Using XSL, how do I sort on a field that is character but really contains numeric data? For instance:
<xsl:for-each select=&quot;./orderlines/orderline&quot; order-by=&quot;linenum&quot; >
creates output sorted as 1,10,11,12,13,14,15,16,17,18,19,20,2,.......
I need to cast the char to an int in XSL - but don't know how to do that.

Any ideas?

Thanks
Tim Ryan
 
You can use the <xsl:sort> element within the <xsl:for-each> element. This allows you to specify the data-type as &quot;number&quot; and so will sort according to that.

So with an xml file:
<root>
<orderlines>
<orderline linenum=&quot;2&quot;>Line 2</orderline>
<orderline linenum=&quot;4&quot;>Line 4</orderline>
<orderline linenum=&quot;5&quot;>Line 5</orderline>
<orderline linenum=&quot;1&quot;>Line 1</orderline>
<orderline linenum=&quot;3&quot;>Line 3</orderline>
<orderline linenum=&quot;6&quot;>Line 6</orderline>
</orderlines>
</root>

The xsl file could look like so:
<?xml version=&quot;1.0&quot;?>

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>

<xsl:template match=&quot;root&quot;>
<xsl:for-each select=&quot;orderlines/orderline&quot;>
<xsl:sort select=&quot;@linenum&quot; data-type=&quot;number&quot;/>
<xsl:value-of select=&quot;.&quot;/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top