开发者

How to find all empty subtrees in XPath

开发者 https://www.devze.com 2023-01-06 15:21 出处:网络
The overall goal is to remove all empty nodes--including nodes which would be made empty if the child empty nodes were removed. The following:

The overall goal is to remove all empty nodes--including nodes which would be made empty if the child empty nodes were removed. The following:

<xsl:template match="*[not(node())]"/>

will remove empty nodes, but will leave empty nodes that had only empty nodes in them prev开发者_JAVA百科iously. I think I need an xpath that will match all nodes that all descendants of which have no attributes and no text nodes (ignoring whitespace).


Use:

<xsl:template match="*[not(.//@*) and not(descendent::text())]"/>

Notice the and operator. Using or does not solve the problem.


I think I found the answer...

<xsl:template match="*[not(.//text() | .//@*)]"/>

Just find all nodes that have no descendant text or attributes, right? Is there something I'm missing such that this won't do what I think it will in some cases? In my simple test, this seemed to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号