XSL if:使用多个测试条件进行测试

时间:2014-01-27 12:34:29

标签: xml xslt

[解决]

感谢@IanRoberts,我不得不在节点上使用normalize-space函数来检查它们是否为空。

<xsl:if test="((node/ABC!='') and (normalize-space(node/DEF)='') and (normalize-space(node/GHI)=''))">
  This worked perfectly fine.
</xsl:if>

[问题]

我正在尝试创建一个xsl条件来检查节点的组合是否为空。我尝试过以下条件,但它们不起作用,是否有人知道如何使其工作

<xsl:if test=" node/ABC!='' and node/DEF='' and node/GHI='' ">
This does not work
</xsl:if>

我也试过

<xsl:when test="((node/ABC!='') and (node/DEF='') and (node/GHI=''))">
This does not work either..
</xsl:when>

还试过

<xsl:if test="(node/ABC!='')>
<xsl:if test="(node/DEF='')>
<xsl:if test="(node/GHI='')">
Nope not working..
</xsl:if>
</xsl:if>
</xsl:if>

我,然后尝试使用单个xsl:if条件,以及下面是观察

<xsl:if test="node/ABC!=''>
**This is working fine**
</xsl:if>

但是,如果我试图搜索空状态,即

<xsl:if test="node/ABC=''>
**This does not work**
</xsl:if>

另外,如果我尝试使用==(double等于),那么它会产生xslt错误。即

<xsl:if test="node/ABC==''>
***This gives a compilation error***
</xsl:if>

我想帮助弄清楚如何获取我的xsl:如果测试工作以检查多个条件。提前致谢。

[编辑]:只是为了在这里更新所有节点都不为空的if条件,当我尝试检查三个空的节点中的任何其他节点时,它不起作用。

例如:

<xsl:if test=" node/ABC!='' and node/DEF!='' and node/GHI!='' ">
This condition works perfectly fine.
</xsl:if>

3 个答案:

答案 0 :(得分:20)

感谢@IanRoberts,我不得不在节点上使用normalize-space函数来检查它们是否为空。

<xsl:if test="((node/ABC!='') and (normalize-space(node/DEF)='') and (normalize-space(node/GHI)=''))">
  This worked perfectly fine.
</xsl:if>

答案 1 :(得分:1)

尝试使用empty()功能:

<xsl:if test="empty(node/ABC/node()) and empty(node/DEF/node())">
    <xsl:text>This should work</xsl:text>
</xsl:if>

这将ABCDEF标识为空,因为它们没有任何子节点(没有元素,没有文本节点,没有处理指令,没有注释)。

但是,正如@Ian指出的那样,你的元素可能不是空的,或者可能不是你的实际问题 - 你没有显示你的输入XML是什么样的。

错误的另一个原因可能是树中的相对位置。这种测试条件的方式仅在周围模板与node的父元素匹配或者迭代node的父元素时才有效。

答案 2 :(得分:1)

仅出于完整性考虑,那些不了解XSL 1的人会选择多个条件。

<xsl:choose>
 <xsl:when test="expression">
  ... some output ...
 </xsl:when>
 <xsl:when test="another-expression">
  ... some output ...
 </xsl:when>
 <xsl:otherwise>
   ... some output ....
 </xsl:otherwise>
</xsl:choose>
相关问题