首页 > 解决方案 > XSLT 变量没有返回任何值

问题描述

我已经声明了 2 个变量并用选择填充它们。之后我想用 ifs 检查它们,但我只发现了两个错误,而且我不知道它们来自哪里。

我试图更改 if 语句,但没有任何效果。

        <root>
            <xsl:choose>
                <xsl:when test="Request/Query/Parameter = 'DateiPfad'">
                    <xsl:variable name="Con1">
                        2001
                    </xsl:variable>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="Con1">
                        2000
                    </xsl:variable>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="Request/Query/Parameter = 'DateiName'">
                    <xsl:variable name="Con2">
                        2001
                    </xsl:variable>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="Con2">
                        2000
                    </xsl:variable>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="$Con1 == 2001 and $Con2 == 2001">
            <xsl:processing-instruction name="ConditionState">
                    2001
                </xsl:processing-instruction>
            </xsl:if>
            <xsl:if test="$Con1 == 2000 and $Con2 == 2000">
                <xsl:processing-instruction name="ConditionState">
                    2000
                </xsl:processing-instruction>
            </xsl:if>
        </root>

我希望 IF 结果会给我 2000 或 2001 作为我在我的过程中需要的条件状态......

标签: xmlxsltxslt-1.0xslt-2.0

解决方案


变量的范围仅限于声明它们的块,这意味着在您的情况下它们仅存在于xsl:when(and xsl:otherwise) 块中,并且在该块之外不可访问。

你能做的,就是把里面xsl:choosexsl:variable

<xsl:template match="/">
    <root>
        <xsl:variable name="Con1">
            <xsl:choose>
                <xsl:when test="Request/Query/Parameter = 'DateiPfad'">2001</xsl:when>
                <xsl:otherwise>2000</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="Con2">
            <xsl:choose>
                <xsl:when test="Request/Query/Parameter = 'DateiName'">2001</xsl:when>
                <xsl:otherwise>2000</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$Con1 = 2001 and $Con2 = 2001">
            <xsl:processing-instruction name="ConditionState">
                2001
            </xsl:processing-instruction>
        </xsl:if>
        <xsl:if test="$Con1 = 2000 and $Con2 = 2000">
            <xsl:processing-instruction name="ConditionState">
                2000
            </xsl:processing-instruction>
        </xsl:if>
    </root>
</xsl:template>   

当然,在您展示的示例中,您根本不需要xsl:variable...

<xsl:template match="/">
    <root>
        <xsl:if test="Request/Query/Parameter = 'DateiPfad' and Request/Query/Parameter = 'DateiName'">
            <xsl:processing-instruction name="ConditionState">
                2001
            </xsl:processing-instruction>
        </xsl:if>
        <xsl:if test="not(Request/Query/Parameter = 'DateiPfad') and not(Request/Query/Parameter = 'DateiName')">
            <xsl:processing-instruction name="ConditionState">
                2000
            </xsl:processing-instruction>
        </xsl:if>
    </root>
</xsl:template> 

推荐阅读