【问题标题】:XSLT Variables does not have any Values backXSLT 变量没有返回任何值
【发布时间】:2019-05-22 13:12:39
【问题描述】:

我已经声明了 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 作为我在我的过程中需要的条件状态...

【问题讨论】:

    标签: xml xslt xslt-1.0 xslt-2.0


    【解决方案1】:

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

    您可以做的是将xsl:choose 放在xsl: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> 
    

    【讨论】:

    • 哇 xD 这太容易了...我很惭愧...我现在要埋葬自己了:D 什么xD 谢谢
    • 请注意您的 XSLT 有语法错误,因为您正在使用 $Con1 == 2000(两个等号)而不是 $Con1 = 2000
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多