简体   繁体   中英

xml parse xpath php

I am having an xml document of this form:

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

and so on. Normally I would parse recursively the file for every node and see if the node has children or not to do some extra html formatting. The problem with this schema is that the children appear even if it is single entry (header of a single chapter for example) or empty.

I am using php and xpath, how can I know that the item/children/ of each node has no children so that I would apply a special formatting?

how can I know that the item/children/ of each node has no children so that I would apply a special formatting?

This XPath expression :

//item/children[not(node())]

selects any children element in the XML document, that is a child of an item and that has no children nodes itself.

To select //item/children elements that do have children nodes themselves, use :

//item/children[node()]

XPath expression children[count(node())>0] return, in a given context, the children element(s) that is(are) not empty.

To have the one(s) that is(are) empty, change the > to = .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM