File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes sort child elements based on its attribute value which is expected to alphanumeric - XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "sort child elements based on its attribute value which is expected to alphanumeric - XSLT" Watch "sort child elements based on its attribute value which is expected to alphanumeric - XSLT" New topic
Author

sort child elements based on its attribute value which is expected to alphanumeric - XSLT

Sharmu Dev
Greenhorn

Joined: Jan 17, 2012
Posts: 3

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: 464
    
    2
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

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

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: 18150
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sort child elements based on its attribute value which is expected to alphanumeric - XSLT
 
Similar Threads
some notes on xml
Problem in HTML to Formatting Objects
XML Notes - IV
Sorting XML nodes?
Incrementing 1 Day in XSLT