Xslt is it ok if we do `select =" $ position + $ jump "`?

I have this code (which works correctly):

 <xsl:template name="CamelChain">
      <xsl:param name="input"/>
      <xsl:param name="position"/>
      <xsl:if test="$position &lt;= string-length($input)">
         <xsl:choose>
         <xsl:when test="substring($input, $position, 1) = '_'">
            <xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

            <xsl:call-template name="CamelChain">
               <xsl:with-param name="input" select="$input"/>
               <xsl:with-param name="position" select="$position + 2"/>
            </xsl:call-template>
         </xsl:when>

         <xsl:otherwise>

            <xsl:value-of select="substring($input, $position, 1)"/>

            <xsl:call-template name="CamelChain">
               <xsl:with-param name="input" select="$input"/>
               <xsl:with-param name="position" select="$position + 1"/>
            </xsl:call-template>
         </xsl:otherwise>
         </xsl:choose>
      </xsl:if>
   </xsl:template>

And I tried to normalize it as such:

   <xsl:template name="CamelChain">
      <xsl:param name="input"/>
      <xsl:param name="position"/>
      <xsl:if test="$position &lt;= string-length($input)">
         <xsl:choose>
            <xsl:when test="substring($input, $position, 1) = '_'">
               <xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
               <xsl:variable name="jump" select="2"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="substring($input, $position, 1)"/>
               <xsl:variable name="jump" select="1"/>
            </xsl:otherwise>
         </xsl:choose>
         <xsl:call-template name="CamelChain">
            <xsl:with-param name="input" select="$input"/>
            <xsl:with-param name="position" select="$position + $jump"/>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

But after I “normalized” it, it no longer works. I suspect this has something to do with the part select="$position + $jump", but I'm not sure what is wrong with it. Does anyone know what is wrong?

+1
source share
2 answers

Your two variables $jumpgo beyond before referencing them .

In XSLT, as in any block-structured language, a variable has a scope from which it is undefined.

    <xsl:when test="substring($input, $position, 1) = '_'">
        <xsl:value-of select=
        "translate(substring($input, $position + 1, 1),
                   'abcdefghijklmnopqrstuvwxyz',
                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        <xsl:variable name="jump" select="2"/>
    </xsl:when>

$jump . , XSLT-, Saxon, .

( $jump).

+1

, $jump . xsl:choose , . , :

<xsl:variable name="char" select="substring($input, $position, 1)" />

<xsl:variable name="jump">
    <xsl:choose>
        <xsl:when test="$char = '_'">2</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<xsl:choose>
    <xsl:when test="$char = '_'">
        <xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$char"/>
    </xsl:otherwise>
</xsl:choose>

<xsl:call-template name="CamelChain">
    <xsl:with-param name="input" select="$input"/>
    <xsl:with-param name="position" select="$position + $jump"/>
</xsl:call-template>

xsl:choose xsl:variable, .

, . .

+4

All Articles