xml XSL if:使用多个测试条件进行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21381055/
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
XSL if: test with multiple test conditions
提问by codingbbq
[Solved]
[解决了]
Thanks to @IanRoberts, I had to use the normalize-space function on my nodes to check if they were empty.
感谢@IanRoberts,我不得不在我的节点上使用 normalize-space 函数来检查它们是否为空。
<xsl:if test="((node/ABC!='') and (normalize-space(node/DEF)='') and (normalize-space(node/GHI)=''))">
This worked perfectly fine.
</xsl:if>
[Problem]
[问题]
I am trying to create a xsl condition to check if combinations of node are empty or not. I have tried below conditions but they do not work, does anyone have an idea as to how to get it working
我正在尝试创建一个 xsl 条件来检查节点的组合是否为空。我已经尝试过以下条件,但它们不起作用,有没有人知道如何让它工作
<xsl:if test=" node/ABC!='' and node/DEF='' and node/GHI='' ">
This does not work
</xsl:if>
I have also tried
我也试过
<xsl:when test="((node/ABC!='') and (node/DEF='') and (node/GHI=''))">
This does not work either..
</xsl:when>
And also tried
也试过了
<xsl:if test="(node/ABC!='')>
<xsl:if test="(node/DEF='')>
<xsl:if test="(node/GHI='')">
Nope not working..
</xsl:if>
</xsl:if>
</xsl:if>
I, then tried with single xsl:if conditions, and below is the observation
我,然后尝试使用单个 xsl:if 条件,下面是观察结果
<xsl:if test="node/ABC!=''>
**This is working fine**
</xsl:if>
However if i try to search for empty condition, i.e
但是,如果我尝试搜索空条件,即
<xsl:if test="node/ABC=''>
**This does not work**
</xsl:if>
Also, if i try with a == (double equal to), then it gives xslt error. i.e
另外,如果我尝试使用 ==(双等于),则会出现 xslt 错误。IE
<xsl:if test="node/ABC==''>
***This gives a compilation error***
</xsl:if>
I would like help in figuring out how to get my xsl:if test working to check for multiple conditions. Thanks in advance.
我想帮助弄清楚如何让我的 xsl:if 测试工作以检查多个条件。提前致谢。
[Edit] : Just to update here that the if condition where all the nodes are not empty works, it does not work when i try to check for any other node out of the three nodes that are empty.
[编辑]:只是在这里更新所有节点都不为空的 if 条件有效,当我尝试检查三个空节点中的任何其他节点时它不起作用。
For eg :
例如:
<xsl:if test=" node/ABC!='' and node/DEF!='' and node/GHI!='' ">
This condition works perfectly fine.
</xsl:if>
采纳答案by codingbbq
Thanks to @IanRoberts, I had to use the normalize-space function on my nodes to check if they were empty.
感谢@IanRoberts,我不得不在我的节点上使用 normalize-space 函数来检查它们是否为空。
<xsl:if test="((node/ABC!='') and (normalize-space(node/DEF)='') and (normalize-space(node/GHI)=''))">
This worked perfectly fine.
</xsl:if>
回答by Mathias Müller
Try to use the empty()function:
尝试使用该empty()功能:
<xsl:if test="empty(node/ABC/node()) and empty(node/DEF/node())">
<xsl:text>This should work</xsl:text>
</xsl:if>
This identifies ABCand DEFas empty in the sense that they do not have any child nodes (no elements, no text nodes, no processing instructions, no comments).
这标识ABC并DEF在这个意义上,他们没有任何的子节点(没有元素,没有文本节点,没有处理指令,无评论)空。
But, as pointed out by @Ian, your elements might not be empty really or that might not be your actual problem - you did not show what your input XML looks like.
但是,正如@Ian 所指出的,您的元素可能真的不是空的,或者这可能不是您的实际问题 - 您没有显示输入 XML 的样子。
Another cause of error could be your relative positionin the tree. This way of testing conditions only works if the surrounding template matches the parent element of nodeor if you iterate over the parent element of node.
另一个错误原因可能是您在树中的相对位置。这种测试条件的方式仅在周围模板与 的父元素匹配node或者您迭代 的父元素时才有效node。

