xslt is it ok if we do `select=“$position + $jump”

2019-07-31 08:52发布

I've got this code (which is working properly):

 <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 attempted to normalize it abit 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've "normalized" it.. it's not working anymore. I suspect its got something to do with the select="$position + $jump" portion but i'm not sure what's wrong with it. does anyone know what's wrong?

标签: xml xslt
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-31 09:14

Your two $jump variables each go out of scope before you reference them.

In XSLT as in any block-structured language a variable has a scope, out of 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>

here you are defining the $jump variable at the very end of its scope and it immediately ceases to exist. This is an obvious error and Some XSLT processors as Saxon even issue a warning message about this.

You have exactly the same problem with the other variable (also named $jump) definition.

查看更多
Ridiculous、
3楼-- · 2019-07-31 09:21

Your problem is that the variable $jump is out of scope. You can't set variables inside a xsl:choose, and expect their value to persist outside. I believe you have to edit the middle section like this:

<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>

The xsl:choose must go within the xsl:variable, not the other way around.

To be honest though, I can't see anything objectionable with your original code. It looks cleaner to me.

查看更多
登录 后发表回答