[xsl] node-set returning functions and their applications

Subject: [xsl] node-set returning functions and their applications
From: Nic Ferrier <nferrier@xxxxxxxxxxxxxxxxxxxx>
Date: 01 Oct 2003 20:52:56 +0100
A colleague and I were working on the following problem: we want to
pass a comma separted list of values to a stylesheet and have the
stylesheet extract bits of the input xml based on the values. 

We knew we could use str:split to find the values and then create
templates from them, something like this:


  <xsl:param name="id_list">1,2,3,4</xsl:param>

  <xsl:template match="/">
     <xsl:for-each select="str:split($id_list, ',')">

        <xsl:variable name="the_id" select="." />

        <xsl:apply-templates select="//el[@id = $the_id]" />

     </xsl:for-each>
  </xsl:template>

But we just couldn't get it to work. The node-set returned by the
xpath expression in the inner apply-templates was empty.

We scratched our heads for some time (and went down several blind
alleys) before realising that the document context within the
for-each is different to the context outside it.

The fix to our problem is to take a node-set pointer from the main
document into the for-each with us:

  <xsl:template match="/">

     <xsl:variable name="the_document" select="//el" />

     <xsl:for-each select="str:split($id_list, ',')">

        <xsl:variable name="the_id" select="." />

        <xsl:apply-templates select="$the_document[@id = $the_id]" />

     </xsl:for-each>
  </xsl:template>


We hope that Google finds this and that other people can find it in
Google.


Nic Ferrier
http://www.tapsellferrier.co.uk


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread