Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL: How best to store a node in a variable and then use it in future xpath expressions?

Tags:

xslt

xpath

I need to be able to store a node set in variable and then perform more filting/sorting on it afterward. All the examples I've seen of this involve either using XSL2 or extensions neither of which are really an option.

I've a list of hotels in my XML doc that can be sorted/filtered and then paged through 5 at a time. I'm finding though I'm repeating alot of the logic as currently I've not found a good way to store node-sets in xsl variable and then use xpath on them for further filtering/sorting.

This is the sort of thing I'm after (excuse the code written of the top of my head so might not be 100%):

<xsl:variable name="hotels" select="/results/hotels[active='true']" />
<xsl:variable name="3_star_or_less" select="/results/hotels[number(rating) <= 3]" />
<xsl:for-each select="3_star_or_less">
  <xsl:sort select="rating" />
</xsl:for-each>

Has anyone got an example of how best to do this sort of thing?

like image 431
Pete Duncanson Avatar asked Dec 23 '09 17:12

Pete Duncanson


2 Answers

Try this example:

<xsl:variable name="hotels" select="/results/hotels[active='true']" />
<xsl:variable name="three_star_or_less"
              select="$hotels[number(rating) &lt;= 3]" />
<xsl:for-each select="$three_star_or_less">
    <xsl:sort select="rating" />
    <xsl:value-of select="rating" />
</xsl:for-each>
like image 126
Rubens Farias Avatar answered Oct 16 '22 08:10

Rubens Farias


There is no problem storing a node-set in a variable in XSLT 1.0, and no extensions are needed. If you just use an XPath expression in select attribute of xsl:variable, you'll end up doing just that.

The problem is only when you want to store the nodes that you yourself had generated in a variable, and even then only if you want to query over them later. The problem here is that nodes you output don't have type "node-set" - instead, they're what is called a "result tree fragment". You can store that to a variable, and you can use that variable to insert the fragment into output (or another variable) later on, but you cannot use XPath to query over it. That's when you need either EXSLT node-set() function (which converts a result tree fragment to a node-set), or XSLT 2.0 (in which there are no result tree fragments, only sequences of nodes, regardless of where they come from).

For your example as given, this doesn't seem to be a problem. Rubens' answer gives the exact syntax.

like image 31
Pavel Minaev Avatar answered Oct 16 '22 06:10

Pavel Minaev