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
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