Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl:sort using an xsl:variable as the sort key

Reply
Thread Tools

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

 
 
jobooker@gmail.com
Guest
Posts: n/a
 
      09-05-2006
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

 
Reply With Quote
 
 
 
 
jobooker@gmail.com
Guest
Posts: n/a
 
      09-05-2006
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

http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-05-2006


(E-Mail Removed) 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
<xslaram 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0])) sajuptpm Python 7 09-08-2010 06:55 PM
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments