我有这个代码(工作正常):
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:if test="$position <= 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>
我试图将其恢复正常化:
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:if test="$position <= 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>
但是在我“正常化”它之后......它已经不再适用了。我怀疑它与select="$position + $jump"
部分有关,但我不确定它有什么问题。有谁知道什么是错的?
答案 0 :(得分:4)
您的问题是变量$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
范围内,而不是相反。
说实话,我看不出你的原始代码有什么异议。它对我来说看起来更干净。
答案 1 :(得分:1)
您的两个$jump
变量在引用之前都超出了范围。
在XSLT中,就像在任何块结构语言中一样,变量有一个范围,未定义范围。
<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
)定义存在完全相同的问题。