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

jobooker

Usenet Archive
Joined
Sep 5, 2006
Messages
2
Reaction score
0
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.getElementsByTagName("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
 

jobooker

Usenet Archive
Joined
Sep 5, 2006
Messages
2
Reaction score
0
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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_Transformations> Opera 9 uses the same API.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top