xml解析xpath php

xml解析xpath php,php,xml,xpath,Php,Xml,Xpath,我有一个以下格式的xml文档: <root> <item title="x"> <children/> </item> <item title="y"> <children> <item title="y1"> <children/> </item> <item title="y1"&

我有一个以下格式的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,如何知道每个节点的项/子项/没有子项,以便应用特殊格式?

xpath表达式
children[count(node())>0]
在给定上下文中返回非空的
子项
元素

要使其中一个为空,请将
更改为
=

如何知道每个节点的项/子项/都没有子项 我会应用一种特殊的格式吗

此XPath表达式

//item/children[not(node())]
//item/children[node()]
选择XML文档中的任何
子元素
,该元素是
的子元素,并且本身没有子节点

要选择本身具有子节点的
//item/children
元素,请使用

//item/children[not(node())]
//item/children[node()]
在这种情况下,不需要使用
count()
,可能会导致效率低下。