Search code examples
xmlxsltxpathnode-set

How to refer to source XML while walking a nodeset?


I have the following XSLT stylesheet (simplified):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="categories">
    <category name="one"/>
    <category name="two"/>
    <category name="three"/>
</xsl:variable>

<xsl:template match="/">
<result>

<xsl:for-each select="exsl:node-set($categories)/category">
<xsl:element name="{@name}">

<!-- THIS IS THE PROBLEMATIC PART -->
    <xsl:for-each select="/input/object">
        <item>
            <xsl:value-of select="."/>
        </item>
    </xsl:for-each>

</xsl:element>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>

This refers to the following source XML document (also simplified):

<?xml version="1.0" encoding="utf-8"?>
<input>
    <object category="one">a</object>
    <object category="one">b</object>
    <object category="two">c</object>
    <object category="one">d</object>
    <object category="three">e</object>
</input>

The reference to the source document produces no result; the output is just empty elements, one for each category:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <one/>
  <two/>
  <three/>
</result>

How can I "fill" the elements with items from the source document?


Just to clarify, the "real" problem behind this is already solved, using a different approach. I am just trying to understand why this approach is not working.


Solution

  • In an xsl:for-each XPaths are interpreted in the context of the selected "document", i.e. / refers to node-set($categories). You can see for yourself by trying the following code:

    <xsl:template match="/">
      <xsl:variable name="root" select="/"/>
      <result>
    
      <xsl:for-each select="exsl:node-set($categories)/category">
        <xsl:element name="{@name}">
    
        <xsl:for-each select="$root/input/object">
            <item>
                <xsl:value-of select="."/>
            </item>
        </xsl:for-each>
    
        </xsl:element>
      </xsl:for-each>
      </result>
    </xsl:template>
    

    It uses the variable root to pass access to the document selected by the template to the inner xsl:for-each loop. (Note: The variable could as well been placed outside of the template.)

    You can double-check that / actually uses the node-set by replacing select="/input/object" by select="/category" in your original code. You will get empty items (one per category) in your elements.