“为什么?”;福!=1“;行为举止不同于;不是(foo=1)“;在xslt中,如果存在';没有foo节点存在吗?
我在XSLT中有这样一个条件:“为什么?”;福!=1“;行为举止不同于;不是(foo=1)“;在xslt中,如果存在';没有foo节点存在吗?,xslt,Xslt,我在XSLT中有这样一个条件: <xsl:if test="foo != 1"> <p>This should be shown if foo doesn't equal one</p> </xsl:if> 它开始像我预期的那样工作:如果没有foo,条件也会成立 有人能解释一下为什么在XSLT中如此吗。检查节点不存在以及没有特定值的最佳方法是什么?使用以下输入 <?xml version="1.0" encoding="UTF
<xsl:if test="foo != 1">
<p>This should be shown if foo doesn't equal one</p>
</xsl:if>
它开始像我预期的那样工作:如果没有foo
,条件也会成立
有人能解释一下为什么在XSLT中如此吗。检查节点不存在以及没有特定值的最佳方法是什么?使用以下输入
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
<foo>1</foo>
</test>
<test>
<foo>0</foo>
</test>
<test>
<a>xxx</a>
</test>
</root>
1.
0
xxx
以及下面的样式表
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//test">
<xsl:choose>
<xsl:when test="not(foo[.!=1])">
<p>aaa</p>
</xsl:when>
<xsl:otherwise>
<p>bbb</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
aaa
bbb
输出是
<?xml version="1.0" encoding="utf-8"?>
<p>aaa</p>
<p>bbb</p>
<p>aaa</p>
aaa
bbb
aaa
您的第一句话是:
if there is a foo element that is not 1
if there is no foo element that is 1
要使其为真,必须存在一个foo元素,否则即使根本没有foo元素,这也是假的
你的第二句话是:
if there is a foo element that is not 1
if there is no foo element that is 1
这是做你想做的事情的正确方法,因为如果根本没有foo元素,这也是正确的,因为这里的“aaa”是Op所拥有的,因为“如果foo不等于1,则应显示”得出了该答案,因为Op需要
,检查节点不存在以及它没有特定值的最佳方法是什么?
我认为他自己发布了解决方案,不是(foo=1)
这样做的,如果没有通常很难把握的双重否定,那么,如果你将
改为
,结果是bbb
,aaa
,和aaa
。这是个不错的建议,但正如@TobiasKlevenz所写的,由于双重否定,对我来说有点复杂。非常感谢。