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 Multiple Transformations per XSL - 1.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Multiple Transformations per XSL - 1.0" Watch "Multiple Transformations per XSL - 1.0" New topic
Author

Multiple Transformations per XSL - 1.0

Priya Bindra
Ranch Hand

Joined: Sep 22, 2009
Posts: 51
Hi All,

I am an absolute novice to XSL and i have a very urgent task at hand which requires multiple sorting and transformation of an xml. The irony is that i can use only single XSL. I have done some googling and read about exsl:node-set function which takes the output of one transofrmation in a variable and can be used for next round of transformation. But unfortunately i have not been able to successfully use it. Can some one guide me.

My first task is to sort a set of child nodes per parent node in ascending order based on an attribute of the child node. And then sort the parent nodes in ascending order based on another attribute of the parent node.

eg:
<parents>

<parent parentPriority='2'>
<child childPriority='20' />
<child childPriority='12' />
</parent>

<parent parentPriority='5'>
<child childPriority='9' />
<child childPriority='6' />
</parent>

</parents>

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Priya Bindra wrote:The irony is that i can use only single XSL.
Who says so? If the best solution is to transform twice, then you should certainly do that.
My first task is to sort a set of child nodes per parent node in ascending order based on an attribute of the child node. And then sort the parent nodes in ascending order based on another attribute of the parent node.
However in this case you don't need to sort twice. You just have your requirements written down in the wrong order. Starting from the top node, you need to sort the parent nodes in whatever order. And for each of those parents, you need to sort its children in whatever order. Only one transformation required.
Priya Bindra
Ranch Hand

Joined: Sep 22, 2009
Posts: 51
XML to be sorted:

---------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore target="crosssell">

<book priority="2">
<title lang="eng">Learning XML</title>
<price cost="1">1</price>
<price cost="2">2</price>
</book>

<book priority="1">
<title lang="eng">Harry Potter</title>
<price cost="3">3</price>
<price cost="4">4</price>
</book>

</bookstore>

---------------------------------------------------------------------------------------------
GOAL: sort in ascending order of priority and descending order of cost

XSL used:

---------------------------------------------------------------------------------------------

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml"/>

<xsl:template match="/bookstore" >
<xsl:for-each select="book">
<xsl:sort select="@priority" order="descending" data-type="number"/>
<!--<xsl:call-template name="price" />-->
<xsl:for-each select="price">
<xsl:sort select="@cost" order="descending" data-type="number"/>
<!--<xsl:copy-of select="."/>-->
</xsl:for-each>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


---------------------------------------------------------------------------------------------

RESULT:

---------------------------------------------------------------------------------------------

<?xml version='1.0' encoding='UTF-8' ?>
<book priority="1">
<title lang="eng">Harry Potter</title>
<price cost="3">3</price>
<price cost="4">4</price>
</book><book priority="2">
<title lang="eng">Learning XML</title>
<price cost="1">1</price>
<price cost="2">2</price>
</book>

---------------------------------------------------------------------------------------------

Priya Bindra
Ranch Hand

Joined: Sep 22, 2009
Posts: 51
as can be seen in the output, only one sorting is working. kindly guide.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Priya Bindra wrote:GOAL: sort in ascending order of priority and descending order of cost

Okay. So you're on the right track, but let's look at how you are sorting:
<xsl:sort select="@priority" order="descending" data-type="number"/>

<xsl:sort select="@cost" order="descending" data-type="number"/>

Priya Bindra
Ranch Hand

Joined: Sep 22, 2009
Posts: 51
i copied the xsl wrongly. i did put proper sorting order as decribed. You can try running this xsl and check the response. (do change desc to asc)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Your code was wrong. You say you ran the right code and got the wrong answer but since you posted the wrong code here it's just as likely that you ran the wrong code there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Transformations per XSL - 1.0
 
Similar Threads
questions on IBM "s sample test
XML Notes - IV
some notes on xml
Sorting JTree Nodes alphabetically
evaluate the expression