我有一个如下形式的xml文档:
<root>
<item title="x">
<children/>
</item>
<item title="y">
<children>
<item title="y1">
<children/>
</item>
<item title="y1">
<children>
<item title="y1.1">
<children/>
</item>
</children>
</item>
</children>
</item>
</root>
通常,我会递归地解析每个节点的文件,看看节点是否有子节点,以进行一些额外的html格式化。这个模式的问题是,即使它是单个条目(例如,单个章节的标题)或空的,子项也会出现。
我使用的是php和xpath,我怎么知道每个节点的项/children/没有子项,这样我就可以应用特殊的格式了?
我怎么知道每个节点的项/children/没有子项,所以我会应用特殊格式吗?
此XPath表达式:
//item/children[not(node())]
选择XML文档中的任何children
元素,该元素是item
的子元素并且本身没有子节点。
要选择本身具有子节点的//item/children
元素,请使用:
//item/children[node()]
XPath表达式children[count(node())>0]
在给定上下文中返回不为空的children
元素。
要使其中一个为空,请将>
更改为=
。