Author
sort child elements based on its attribute value which is expected to alphanumeric - XSLT
Sharmu Dev
Greenhorn
Joined: Jan 17, 2012
Posts: 3
posted Jan 18, 2012 06:00:43
0
Hi,
I need to sort child elements in a xml file based on its attribute value which is expected to alphanumeric using XSLT.
Please give some idea.
In the below xml file I need to sort H2 using its @PNO
Input file:
<WINDOW>
<H2 PNO="10,888a">...</H2>
<H2 PNO="10,888">...</H2>
<H2 PNO="10,887a">...</H2>
<H2 PNO="10,887">...</H2>
....
<H2 PNO="1000">...</H2>
</WINDOW>
<WINDOW> transforms to <asselmbly> and <H2> transforms to <include>
Expected output should be
<assembly>
<include cite="1000"/>
....
<include cite="10,887"/>
<include cite="10,887a"/>
<include cite="10,888"/>
<include cite="10,888a"/>
</assembly>
g tsuji
Ranch Hand
Joined: Jan 18, 2011
Posts: 225
You should have read in every tutorial discussing sorting in xslt 1.0 that xsl:sort can appear in two kinds of structure. This is what they want to say.
[1] In xsl:apply-templates
Test you skill in making the template for H2.
[2] In xsl:for-each
Sharmu Dev
Greenhorn
Joined: Jan 17, 2012
Posts: 3
posted Feb 14, 2012 01:45:44
0
Hi g tsuji,
Thanks for your response. I tried the way you gave <xsl:sort select="@PNO" order="ascending" data-type="text" /> but little bit modified as <xsl:sort select="if(contains(@PNO,',')) then replace(@PNO,',','') else @PNO" order="ascending" data-type="text" /> it worked for the scenario which i showed in my post with H2/@PNO values (10,888a, 10,888, 10,887a, 10,887, 1000) and when I added another H2 with @PNO="2000" it didnt work.
My code as of now:
<xsl:template match="WINDOW">
<assembly>
<xsl:apply-templates>
<xsl:sort select="if(contains(@PNO,',')) then replace(@PNO,',','') else @PNO" order="ascending" data-type="text" />
</xsl:apply-templates>
</assembly>
</xsl:template>
<xsl:template match="H2">
<include area="h2area" num="{@PNO}"/>
</xsl:template>
Input:
<?xml version="1.0" encoding="UTF-8"?>
<WINDOW>
<H2 PNO="10,888a">...</H2>
<H2 PNO="10,888">...</H2>
<H2 PNO="10,887a">...</H2>
<H2 PNO="10,887">...</H2>
<H2 PNO="1000">...</H2>
<H2 PNO="2000">...</H2>
</WINDOW>
Output:
<?xml version="1.0" encoding="utf-8"?>
<assembly>
<include area="h2area" num="1000"/>
<include area="h2area" num="10,887"/>
<include area="h2area" num="10,887a"/>
<include area="h2area" num="10,888"/>
<include area="h2area" num="10,888a"/>
<include area="h2area" num="2000"/>
</assembly>
Sharmu Dev
Greenhorn
Joined: Jan 17, 2012
Posts: 3
posted Feb 14, 2012 02:23:08
0
Hi g tsuji,
I further tried on sorting the attribute value by separating its numeric and alphabets as below it worked
My code:
Input:
Output:
Thanks please provide comments in order to improvement my implementation.
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
I put your XML into XML code tags, so it would be easier for people to read.
As for improvements: it seems to me that
is equivalent to
is it not? This change would make the code easier to read as well.
subject: sort child elements based on its attribute value which is expected to alphanumeric - XSLT