xml - 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 作为我在我的过程中需要的条件状态......
解决方案
变量的范围仅限于声明它们的块,这意味着在您的情况下它们仅存在于xsl:when
(and 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>
推荐阅读
- javascript - 没有节点的包裹是可能的吗?
- selenium - 从 Selenium 中的 CodeMirror 清除文本
- wordpress - 会员访问 Wordpress
- python - 使用 BeautifulSoup 和 Regex 提取文本
- asp.net-core - .Net Core 等效于 CryptoJS.AES.encrypt
- boost - boost::dll::import c'tor 调用
- ruby - Logstash 过滤器:聚合嵌套数组
- php - laravel 进程无法启动 [Le chemin d'acc?s sp?cifi?est introuvable。发送邮件时
- laravel - 如何在 laravel 中 foreach in store 方法?
- html - 重定向到聊天whatsapp jquery