• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

xls:sort question

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a DOM object which I would like to sort various ways. The DOM object gets created after the user submits a form and looks something like this, with the values for sort1, sort3, and sort3 changing based on what was submitted on a previous form (i.e. the user gets to decide what order he wants the output in):
<orders>
<sorts><sort1>location</sort1><sort2>ID</sort2>
<sort3>date</sort3>
</sorts>
<order><location>Texas</location><ID>12</ID>
<date>12/01/2000</date></order>
<order><location> ... many more orders ...
</orders>
In my xslt document at the top I save the sort values in variables:
<xsl:variable name="sort1">
<xsl:value-of select="/orders/sorts/sort1" />
</xsl:variable>
<xsl:variable name="sort2">
<xsl:value-of select="/orders/sorts/sort2" />
</xsl:variable>
<xsl:variable name="sort3">
<xsl:value-of select="/orders/sorts/sort3" />
</xsl:variable>
Then I have other xslt to display and turn the xml into HTML:
<xsl:template match="orders">
... bunch of html
<xsl:for-each select="order">
<xsl:sort select = "$sort1"/>
<xsl:sort select = "$sort2"/>
<xsl:sort select = "$sort3"/>
<xsl:value-of select="Location"/></FONT></TD>
... many more lines
</xsl:for each>
</xsl:template>
The problem is that no matter what is put between the <sort> tags, the HTML output always gets created in the same order. Why aren't the XSL sort tags working to sort the information so that it is displayed in the correct order? If I hardcode values, like:
<xsl:sort select="Location" />
it works, so it looks like somehow it is not interpreting the variables. And the variables do have values because I have printed them out and checked them.
Any help would be greatly appreciated. I'm pretty new at this XML/XSL stuff!
Thanks for the help!
Brian
bcnice@mindspring.com
[This message has been edited by Brian Nice (edited January 12, 2001).]
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone haveany ideas on this? Sorry, but I need help to get this project done!
Thanks
Brian
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,
How about adding the datatype qualifier to your sort statement?
Example -
<xsl:sort data-type="number" select="$sort1"/>
Let us know if this works!
Ajith
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That didn't seem to have any affect. When looking at the specs for XSL, a string expression can be used for the select:
<xsl:sort
select = string-expression
lang = { nmtoken }
data-type = { "text" | "number" | qname-but-not-ncname }
order = { "ascending" | "descending" }
case-order = { "upper-first" | "lower-first" } />
It seems strange that a variable housing a string would not be evaluated correctly. Is there another way that I can accomplish the same thing?
Brian
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a solution to this too. Maybe this will be helpful to someone in the future. The following works:
<xsl:for-each select="order">
<xsl:sort data-type="text" select="*[name()=$sort1]" order="ascending"/>
<xsl:sort data-type="text" select="*[name()=$sort2]" order="ascending"/>
<xsl:sort data-type="text" select="*[name()=$sort3]" order="ascending"/>
I got this from the following link which has been really helpful! http://www.dpawson.co.uk/xsl/sect21.html
Thanks
Brian
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting.
Have you tried the solution without the data-type attribute?? I'm just curious.
Thanks for the URL, it will keep me busy for sometime

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the data-type attribute, it defaults to the "text" type, and still does the sorting correctly.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I perform the same task if my variable is referencing an attribute?
For example:
XML:
<Members>
<Member
EMail="jacky@tech.com"
FirstName="Jacky"
ID="jchau"
Surname="Chau" />
<Member
EMail="will@tech.com"
FirstName="Will"
ID="wbudik"
Surname="Budik" />
<Member
EMail="mel@tech.com"
FirstName="Melany"
ID="mcox"
Surname="Cox" />
</Members>
XSL:
<xsl :param name="ordering">Surname</xsl :param>
<xsl:for-each select="/Config/Member">
<xsl:sort order="ascending" select="$ordering"/>
<tr><td><xsl:value-of select="./@Surname"/></td>
<td><xsl:value-of select="./@FirstName"/></td>
<td><xsl:value-of select="./@EMail"/></td>
</tr>
</xsl:for-each>
It works okay if I use the above technique for a node but the attrbiute won't work unless I put the actual name. (name()==$ordering doesn't work for an attribute)
Cheers,
Anthony Ikeda

[This message has been edited by Ajith Kallambella (edited January 24, 2001).]
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put in your sort statement:
<xsl:sort order="ascending" select="./@Surname"/>
or do you need a variable to change which attribute will get sorted on?
 
Anthony Ikeda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to use a variable/parameter as it means I can decide at runtime which attribute I want to sort by.
I would prefer to have a different format to the code, unfortunately the software we develop is starting to show a bit of age and does not use XML the way it should.
I can easily use the ./@Surname etc without a hitch.
Anyone able to do this yet?
Cheers,
Anthony Ikeda
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using:
<xsl:for-each select="Member">
<xsl:sort select="@*[name(.)=$paramName]"/>
</xsl:for-each
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!