Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

xml - XSL / XPath expression to check if a node contains at least one non-empty child

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>

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

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: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  /*/node/*[text()]

If you want to exclude any element that has only whitespace-only text children, use:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...