XSLT中的数字变量/参数

Svi*_*ish 1 xslt variables parameters tunneling

我有这个XSLT:

<xsl:template match="/">

    <xsl:variable name="errorCount" select="count($orders/*[1]/cm:Error)" />

    <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="errorCount" select="$errorCount" tunnel="yes" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="status">
    <xsl:param name="errorCount" tunnel="yes" />
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="$errorCount > 0">
                <xsl:text>ERROR</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>OK</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

隧道和所有似乎都工作,但转换失败,出现以下错误:

'>'的第一个操作数的必需项类型是数字; 提供的值具有项类型xs:string

我首先在模板中使用了变量声明,然后它才能正常工作.移动它因为我需要在其他模板中使用相同的计数.

如何/在哪里声明这个变量/参数实际上是一个数字?

Tdd*_*ust 7

由于您使用的是XSLT 2.0,因此您还应该as在模板中的xsl:param中添加一个属性.例如(根据您需要的结果数量,您可能必须使用不同的值,例如,如果您将拥有包含小数的值;您还需要根据Michael Kay的观点更正隧道值) :

<xsl:param name="errorCount" tunnel="yes" as="xs:integer" />
Run Code Online (Sandbox Code Playgroud)

如果转换无法转换为as类型(在本例中为整数),则转换将失败.Eero的解决方案可能看起来更清晰,因为您仍然需要检查该值是否大于零.但是,因为您使用的是XSLT 2.0,所以最佳做法是键入您的参数/变量.