Re: [xsl] xslt 1, node sets in variables

Subject: Re: [xsl] xslt 1, node sets in variables
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 31 Jul 2009 18:15:43 -0400

At 05:13 PM 7/31/2009, you wrote:
When I do what you suggested, it doesn't generate any errors, but I still don't get the result as a node-set. The copy-of statement dumps the xml of the tree to the output. Also, count(exsl:node-set($projects)) returns 1 (It should return 26 when there is no filter).

Sorry, I forgot about this crucial detail.

The data object being converted to the node set is a result tree fragment. As such, it has a root. So when you count it, or rather the node set resulting from converting it, (the count() function counts a set of nodes), you get 1. It's a little tree.

You are probably looking for count(node-set($projects)/*).

You could also clarify things by using an extra variable, so:

<xsl:variable name="projects-rtf">
    <xsl:when test="$active_filter = 'active'">
      <xsl:copy-of select="project[@active!=0]"/>
    <xsl:when test="$active_filter = 'archived'">
      <xsl:copy-of select="project[@active=0]"/>
    <xsl:when test="$active_filter = 'new'">
      <xsl:copy-of select="project[@new=1]"/>
      <xsl:copy-of select="project"/>

<xsl:variable name="projects" select="exsl:node-set($projects-rtf)/*"/>

<xsl:copy-of select="$projects"/>

  <xsl:when test="count($projects)>10">

In this case, the variable $projects-rtf contains a result tree fragment, and $projects contains copies (as nodes) of the actual 'project' elements.

Now, all this having been said, I'm going to take another step back to see whether you even need an RTF to begin with. Why not just bind the project elements themselves to your variable, instead of making copies of them?

Assuming things are not more complicated than you've shown, you might be able to do this as:

<xsl:variable name="projects"
  select="project[$active_filter = 'active'][@active!=0] |
          project[$active_filter = 'archived'][@active=0] |
          project[$active_filter = 'new'][@new=1] |
          project[not($active_filter = 'active' or
                      $active_filter = 'archived' or
                      $active_filter = 'new')]"/>

Then you don't even need to use the node-set() function, since the variable is already a node set (this time the actual nodes from the source tree, not copies).

This works by using XPath predicates to emulate your conditional statements. And while it may be more cumbersome to read (if you don't prefer your logic in XPath anyway: some do), it simplifies things considerably both in your code and in the processing it specifies.

But note I say "might" work since these conditionals are not actually mutually exclusive. For example, if you have a project[@new=1][@active=0], you will get it both for your 'new' filter and your 'archived' filter. You can deal with this either by controlling your input data to preclude such anomalous cases, or by extending your filtering logic further.

Keep asking about mysterious points, as this represents a big advance.


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread