简体   繁体   中英

PHP - DOMXPATH how to return ONLY direct descendants

I am trying to use DOMXPath to get a list of child elements in an XSD file that are direct descendants of an element called 'Item'. However using the 2 xpath options listed below, it always includes grandchildren in the results aswell as child nodes

cheers

$dom = new DOMDocument();
$dom->load('xsdfile.xsd');

$xpath = new DOMXPath($dom);

//$xpath_str = '//xs:element[@name="Item"]/descendant::xs:element';
$xpath_str = '//xs:element[@name="Item"]//xs:element';

$results = $xpath->evaluate($xpath_str);

Under element there is complexType , then sequence , choice or all , and then the actual child elements, so the XPath should be:

//xs:element[@name="Item"]/xs:complexType/*/xs:element

( * = any element) or

//xs:element[@name="Item"]/*/*/xs:element

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