XSL/XPath 表达式来检查一个节点是否包含至少一个非空子节点 [英] XSL / XPath expression to check if a node contains at least one non-empty child

查看:28
本文介绍了XSL/XPath 表达式来检查一个节点是否包含至少一个非空子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一个 XML 节点是否至少有一个非空子节点.应用于此 XML 的表达式应返回 true

I need to check if an XML node has at least one non-empty child. Applied to this XML the expression should return true

<xml>
    <node>
       <node1/>
       <node2/>
       <node3>value</node3>
    </node>
</xml>

我尝试使用这个表达式:<xsl:if test="not(/xml/node/child::* = '')"> 但它似乎检查是否 所有孩子都不是空的.

I tried to use this expression: <xsl:if test="not(/xml/node/child::* = '')"> but it seems to check if all children are not empty.

如果至少一个元素不为空,我如何编写返回true的表达式?有没有办法在不创建另一个模板来迭代节点 chldren 的情况下做到这一点?

How can I write an expression which returns true if at least one element is not empty? Is there a way to do this without creating another template to iterate over node chldren?

UPD:我正在考虑计算非空节点,例如
test="count(not(/xml/node/child::* = '')) &gt; '0'"
但不知何故不能让它正常工作.这个表达式不是一个格式良好的表达式.

UPD: I'm thinking of counting non-empty nodes like
test="count(not(/xml/node/child::* = '')) &gt; '0'"
but somehow just can't make it work right. This expression is not a well-formed one.

推荐答案

更准确、更简单、更高效(无需使用count()函数):

More accurate, simpler and more efficient (no need to use the count() function):

  /*/node/*[text()]

如果您想排除任何只有空白文本子项的元素,请使用:

  /*/node/*[normalize-space()]

这篇关于XSL/XPath 表达式来检查一个节点是否包含至少一个非空子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆