Janne
Janne

Reputation: 39

XSLT 2.0 set a variable with for-each loop

I like to create a variable which contains the full PACKAGE name path for corresponding element. (just to be able to compare paths later)

Now I have XSLT structure:

<xsl:for-each select="ancestor-or-self::*">
   <xsl:if test="local-name()='PACKAGE'">
      <xsl:text>/</xsl:text>
      <xsl:value-of select="NAME/."/>
      <xsl:if test="position() != last()">
      </xsl:if>
   </xsl:if>
</xsl:for-each> 

This above structure results me the correct path, but when I try to put that output to the variable (by placing the <xsl:variable name="myPath"> and </xsl:variable> outside the foreach) variable stays empty.

So why in:

<xsl:variable name="myPath">
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:if test="local-name()='PACKAGE'">
     <xsl:text>/</xsl:text>
     <xsl:value-of select="NAME/."/>
       <xsl:if test="position() != last()">
       </xsl:if>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

$myPath remains empty? What I am missing here?

Upvotes: 1

Views: 607

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167581

Make sure you use that variable somewhere later in your code as otherwise the XSLT processor is free to not execute the code at all. So without seeing more information I assume that Saxon is simply not executing the value of the variable as you don't use it.

Upvotes: 2

Related Questions