Ho bisogno di ottenere il genitore di un nodo particolare in php. Sto usando DomDocument e Xpath. mio xml è questo:Ottieni il nodo genitore Xpath con DomDocument PHP?
<ProdCategories>
<ProdCategory>
<Id>138</Id>
<Name>Parent Category</Name>
<SubCategories>
<ProdCategory>
<Id>141</Id>
<Name>Category child</Name>
</ProdCategory>
</SubCategories>
</ProdCategory>
</ProdCategories>
il codice PHP:
$dom = new DOMDocument();
$dom->load("ProdCategories_small.xml");
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//ProdCategory/Id[.="141"]/parent::*')->item(0);
print_r($nodes);
La stampa è:
DOMElement Object (
[tagName] => ProdCategory [schemaTypeInfo] => [nodeName] => ProdCategory [nodeValue] => 141 Category child [nodeType] => 1 [parentNode] => (object value omitted) [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => (object value omitted)
Il [parentNode]
è (object value omitted)
, perché? Vorrei avere
<Id>138</Id>
<Name>Parent Category</Name>`
L'ultimo nodo impostato nella domanda denota l'output desiderato? L'espressione Xpath può essere riscritta come '// ProdCategory [Id [. =" 141 "]]', funziona? – collapsar
@collapsar grazie per la risposta. No, non funziona. L'ultimo nodo nella mia domanda è quello che voglio. – Eliana
@collapsar - o anche '// ProdCategory [Id =" 141 "]' –