xml 在 XSLT 中,如何测试变量是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1299808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In XSLT how do you test to see if a variable exists?
提问by rjzii
When using XSLT how do you test to see if a locally scoped variable exists, or is this even possible?
使用 XSLT 时,您如何测试以查看局部范围的变量是否存在,或者这是否可能?
回答by Robert Rossney
Considering the XSLT stylesheet as an XML DOM, a variable declaration element makes the variable visible to all following siblings and their descendants. This allows XSLT processors to statically analyze any XPath containing a variable reference to see if the variable exists; if the variable declaration exists on the preceding-sibling or ancestor axis, the variable reference is legal, otherwise it's not.
将 XSLT 样式表视为 XML DOM,变量声明元素使该变量对所有后续同级及其后代可见。这允许 XSLT 处理器静态分析任何包含变量引用的 XPath 以查看变量是否存在;如果变量声明存在于前兄弟轴或祖先轴上,则变量引用是合法的,否则不是。
Note that this is entirely dependent on the structure of the XSLT, not the structure of the XML it's processing. The XSLT processor can and should produce an error if an XPath expression uses a variable that doesn't exist.
请注意,这完全取决于 XSLT 的结构,而不是它正在处理的 XML 的结构。如果 XPath 表达式使用不存在的变量,则 XSLT 处理器可以并且应该产生错误。
There's no way to check for this condition inside XSLT because this condition isn't legal within XSLT. The sitauation you described in your comment - "The idea is to set a flag variable if something is output and later on display a different message if nothing was output." - actually should result in a syntax error. For instance, if you do something like this:
无法在 XSLT 中检查此条件,因为此条件在 XSLT 中不合法。您在评论中描述的情况 - “这个想法是在输出某些内容时设置一个标志变量,然后在没有输出任何内容时显示不同的消息。” - 实际上应该导致语法错误。例如,如果您执行以下操作:
<xsl:if test="some_condition">
<!-- produce output here -->
<xsl:variable name="flag">true</xsl:variable>
</xsl:if>
<!-- time passes -->
<xsl:if test="$flag='true'>
<!-- wouldn't it be nice? -->
</xsl:if>
you'll get a syntax error: the second xsl:ifelement is neither a following sibling of the variable declaration nor one of their descendants.
你会得到一个语法错误:第二个xsl:if元素既不是变量声明的后续兄弟元素,也不是它们的后代元素之一。
Here's a technique I use a fair amount - this produces variable output based on a variety of different conditions that you don't want to re-check later:
这是我经常使用的一种技术 - 这会根据您不想稍后重新检查的各种不同条件产生可变输出:
<xsl:variable name="output">
<xsl:if test="$condition1='true'">
<p>condition1 is true</p>
</xsl:if>
<xsl:if test="$condition2='true'">
<p>condition2 is true</p>
</xsl:if>
<xsl:if test="$condition3='true'">
<p>condition3 is true</p>
</xsl:if>
</xsl:variable>
<!-- we've produced the output, now let's actually *output* the output -->
<xsl:copy-of select="$output"/>
<!-- time passes -->
<xsl:if test="normalize-space($output) != ''">
<p>This only gets emitted if $output got set to some non-empty value.</p>
</xsl:if>
回答by Tomalak
Asking this question indicates that you did not fully grasp the key point of XSLT. :-)
问这个问题说明你没有完全掌握XSLT的关键点。:-)
It's declarative: nothing can exist unless you declare it. You declare a variable, then it's there, you don't, then it's not.
它是声明性的:除非你声明它,否则任何东西都不可能存在。你声明一个变量,然后它就在那里,你没有,那么它就没有。
Not once will there be the point where you have to wonder, while coding, if a certain variable exists.
在编码时,您不会再怀疑某个变量是否存在。
XSLT has strict scoping rules, variables exist only within the scope of their parent element, (and not all elements can contain variables to begin with). Once you leave the parent element, the variable is gone.
XSLT 有严格的范围规则,变量只存在于其父元素的范围内,(并不是所有元素都可以包含变量开始)。一旦离开父元素,变量就消失了。
So unless you specify your question/intent some more, the only valid answer is that the question is wrong. You cannot and do not need tocheck if a variable exists at run-time.
因此,除非您再详细说明您的问题/意图,否则唯一有效的答案是问题是错误的。您不能也不需要在运行时检查变量是否存在。
回答by StarSignLeo
XSL variables are strictly scoped, so you can't access them in sibling nodes, only in children.
If you are dealing with params, you can use a global <xsl:param />.
XSL 变量有严格的范围,因此您不能在同级节点中访问它们,只能在子节点中访问它们。如果您正在处理参数,则可以使用全局<xsl:param />.
See: http://www.stylusstudio.com/xsllist/199911/post30020.html
参见:http: //www.stylusstudio.com/xsllist/199911/post30020.html
回答by Michael Krelin - hacker
I don't think it's possible, but you're not likely to ever need it, because the variable doesn't exist unless you've declared it.
我认为这是不可能的,但您可能永远不需要它,因为除非您声明该变量,否则它不存在。
回答by Karol
Best and fast idea to check value if it's exist is to check it length
检查值是否存在的最佳和快速的想法是检查它的长度
<xsl:if test="string-length(value/to/check)=0">
</xsl:if>
回答by Kezzer
If you have a variable, you can check it has something, or it "exists" by doing something like the following:
如果您有一个变量,您可以通过执行以下操作来检查它是否包含某些内容,或者它是否“存在”:
<xsl:choose>
<xsl:when test="$myvar">
This variable exists!
</xsl:when>
<xsl:otherwise>
The variable doesn't exist :(
</xsl:otherwise>
</xsl:choose>
As for its validity I cannot be certain. I will tell you however, that I do this in some of our systems at work ;)
至于它的有效性我不能确定。然而,我会告诉你,我在我们工作的一些系统中这样做;)

