473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

xsl:sort using an xsl:variable as the sort key

I'm having issues sorting. The short description is, how do I set the
select attribute of xsl:sort to be the value of an xsl:variable? The
longer description follows:

What I want to do is to be able to make a data table sortable by
different headers. Right now, I've got javascript that lets you click
on a column header, and then it changes the DOM of the xsl file
(changes the select attribute of the xsl:sort element) and reapplies
the stylesheet to the xml file, and presents the newly resorted data.
However, the xpath for the select attribute gets ugly.

This is what it looks like now:
<xsl:sort select="item[key='main_contact']/value"/>

So my javascript is:
var sort_node=this.xslStylesheet.getElementsByTagName( "sort").item(0);
sort_xpath="item[key='" + my_sort_param + "']/value";//ugly, JS has to
know a lot about xpath
sort_node.setAttribute("select",sort_xpath);

I'd rather my javascript be more like:
var
sort_param_node=this.xslStylesheet.getElementsByTa gName("variable").item(0);
sort_node.setAttribute("select",my_sort_param); //prettier, JS just
knows a sort key, not the whole xpath

And so I think my xsl should be:
<xsl:variable name="sort_param" select="'main_contact'"/>
<xsl:sort select="item[key='$sort_param']/value"/>

But that doesn't work. I can't even get this to work:
<xsl:variable name="sort_param"
select="item[key='main_contact']/value"/>
<xsl:sort select="$sort_param"/>

I'm pretty confused at this point. I'm not sure where I'm going wrong.
It could be syntax, it might be misunderstanding how the variable works
with string literals vs element tree fragments, or...something else.
Any help is appreciated, thanks,
-John

Sep 5 '06 #1
2 2846
So apparently this does work afterall:
<xsl:variable name="sort_param" select="'name'"/>
<xsl:sort select="item[key=$sort_param]/value" />

Though I tried something like this first, and it blew up. Oh well,
problem solved for now,
-John

jo******@gmail.com wrote:
I'm having issues sorting. The short description is, how do I set the
select attribute of xsl:sort to be the value of an xsl:variable? The
longer description follows:

What I want to do is to be able to make a data table sortable by
different headers. Right now, I've got javascript that lets you click
on a column header, and then it changes the DOM of the xsl file
(changes the select attribute of the xsl:sort element) and reapplies
the stylesheet to the xml file, and presents the newly resorted data.
However, the xpath for the select attribute gets ugly.

This is what it looks like now:
<xsl:sort select="item[key='main_contact']/value"/>

So my javascript is:
var sort_node=this.xslStylesheet.getElementsByTagName( "sort").item(0);
sort_xpath="item[key='" + my_sort_param + "']/value";//ugly, JS has to
know a lot about xpath
sort_node.setAttribute("select",sort_xpath);

I'd rather my javascript be more like:
var
sort_param_node=this.xslStylesheet.getElementsByTa gName("variable").item(0);
sort_node.setAttribute("select",my_sort_param); //prettier, JS just
knows a sort key, not the whole xpath

And so I think my xsl should be:
<xsl:variable name="sort_param" select="'main_contact'"/>
<xsl:sort select="item[key='$sort_param']/value"/>

But that doesn't work. I can't even get this to work:
<xsl:variable name="sort_param"
select="item[key='main_contact']/value"/>
<xsl:sort select="$sort_param"/>

I'm pretty confused at this point. I'm not sure where I'm going wrong.
It could be syntax, it might be misunderstanding how the variable works
with string literals vs element tree fragments, or...something else.
Any help is appreciated, thanks,
-John
Sep 5 '06 #2


jo******@gmail.com wrote:

And so I think my xsl should be:
<xsl:variable name="sort_param" select="'main_contact'"/>
<xsl:sort select="item[key='$sort_param']/value"/>
You would need
<xsl:sort select="item[key=$sort_param]/value"/>
to have $sort_param interpreted as a variable. Your '$sort_param' is
simply a string literal which contains the text '$sort_param'.

And instead of using an xsl:variable and manipulating the stylesheet you
should use a top level
<xsl:param name="sort_param" select="'main_contact'"/>
and then use the API your XSLT processor exposes to script to set that
parameter as needed.
Mozilla's API is described here:
<http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Tran sformations>
Opera 9 uses the same API.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jarle Presttun | last post by:
Hi, Sometimes I display values by combining information from different sections in the xml, like I do with gradeText in the student template bellow. Is it possible to sort on gradeText when I...
1
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted...
3
by: Tjerk Wolterink | last post by:
I posted my problem earlier, but i simplified the examples, and i know what the cause of the problem is, but i dont know the solution, my xml file: <?xml version="1.0" encoding="ISO-8859-1"?>...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
1
by: aerotops | last post by:
Hi, I am trying to sort something using XSLT. I am going to give examples. Original.xml <Root> <Car> <Name>Ford</Name> <DealerRating>3</DealerRating>
0
by: Nicolas | last post by:
Hi, I've been working on this problem for a couple of days and now i have no more ideas. first, this problem only happens with jdk 1.5, everything was fine with jdk1.4. I'm using a...
10
by: William Krick | last post by:
I am writing an XSL transform that converts XML data about vehicles into XML data that will fill printed forms. The default form can handle up to 5 vehicles which I handle using subscripts... ...
2
by: ajc308 | last post by:
I have an XML document that looks like the following: <root name="PlanRepository"> <directory name="connoraj"> <directory name="single_run1"> <file>insidebox.txt</file> ...
7
by: otis | last post by:
Hi all, This is a small issue to make things prettier, but we all know how important that can be! I had an xsl:if to check if a node was the last one in a collection of nodes and if it was a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.