문제

I was wondering how code is executed when you're in a template and call another template that retrieves a value and then you continue processing the first template and using that value capture by the other template. Can anyone provide a working example of this setup?

An example might be:

<xsl:template name="test">

    <xsl:choose>

        <xsl:when test="boolean(.)">

            <xsl:variable name="list" select="'want to store this and return to previous template'"/>
        </xsl:when>
        <xsl:otherwise/>

    </xsl:choose>

</xsl:template>

<xsl:template match="/">

<file_item_nbr>
     <xsl:call-template name="test">
 </xsl:call-template>
     <xsl:value-of select="$test"/>
</file_item_nbr>

</xsl:template>

My real case goes much further than what I've displayed as an example. I want to be able to get the parent nodes number after the hyphen(-) and pass that integer value back to the original template to then increment the variable by a specific number.

도움이 되었습니까?

해결책

You have a simple variable scoping problem.

Variables are scoped to their immediate parent. They do not exist outside of it.

Just reverse your approach. You want the variable contents depend a template call, after all. The variable itself must be declared in the same spot where you want to use it.

<xsl:template name="test">
  <xsl:choose>
    <xsl:when test="boolean(.)">
      <xsl:value-of select="'want to store this and return to previous template'" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="'maybe I want to store something else here'" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

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

    <xsl:variable name="result">
      <xsl:call-template name="test" />
    </xsl:variable>

    <xsl:value-of select="$result"/>
  </file_item_nbr>
</xsl:template>

Note that the current node does not change when you do xsl:call-template>, i.e. the meaning of . stays the same as in the calling template.


The two ways to modify a result of <xsl:call-template> are either

<xsl:variable name="temp">
  <xsl:call-template name="test" />
</xsl:variable>

<xsl:variable name="result">
  <xsl:value-of select="$temp + 2" />
</xsl:variable>

or, a bit nicer by nesting variables:

<xsl:variable name="result">
  <xsl:variable name="temp">
    <xsl:call-template name="test" />
  </xsl:variable>
  <xsl:value-of select="$temp + 2" />
</xsl:variable>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top