• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Transformations per XSL - 1.0

 
Priya Bindra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as can be seen in the output, only one sorting is working. kindly guide.
 
Paul Clapham
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic