9

I need to get the parent of a particular node in php. I'm using DOMDocument and XPath. My XML is this:

<ProdCategories>
<ProdCategory>

    <Id>138</Id>

    <Name>Parent Category</Name>

    <SubCategories>

        <ProdCategory>

            <Id>141</Id>

            <Name>Category child</Name>

        </ProdCategory>
   </SubCategories>
</ProdCategory>
</ProdCategories>

The php code:

$dom = new DOMDocument();
$dom->load("ProdCategories_small.xml");
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//ProdCategory/Id[.="141"]/parent::*')->item(0);
print_r($nodes); 

The print is:

 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)

The [parentNode] is (object value omitted), why? I would get

    <Id>138</Id>

    <Name>Parent Category</Name>`
12
  • Does the last node set in your question denote the desired output ? Your Xpath expression can be rewritten as //ProdCategory[Id[.="141"]], does that work ? Commented Jul 10, 2015 at 8:22
  • @collapsar thank you for your response. No, doesn't work. The last node in my question it is what I want. Commented Jul 10, 2015 at 8:25
  • 2
    @collapsar - or even //ProdCategory[Id="141"] Commented Jul 10, 2015 at 8:27
  • 2
    The following xpath expression produces the result you indicated: //ProdCategory[Id='141']/ancestor::ProdCategory/child::*[name() = 'Id' or name()='Name']. Commented Jul 10, 2015 at 8:38
  • 2
    In the context of the matched ProdCategory of your xpath expression, the parent node is SubCategories. You have to use the ancestor axis to reach the outer Prodcategory - see my previous comment. Commented Jul 10, 2015 at 8:40

2 Answers 2

5

The [parentNode] is (object value omitted), why?

This is because you use the print_r function and it creates such an output (via an internal helper function of the dom extension). The line in code which is creating this is:

print_r($nodes);

The string "(object value omitted)" is given by the DOMNode when either print_r or var_dump are used on it. It tells you, that the object value of that field (named parentNode) is not displayed but omitted.

From that message you could conclude that the field has a value that is an object. A simple check for the class-name could verify this:

echo get_class($nodes->parentNode), "\n"; # outputs  "DOMElement"

Compare that with fields which are an integer or an (empty) string:

[nodeType] => 1
...
[prefix] => 
[localName] => ProdCategory

So I hope this clears it up for you. Just access the field to get the parent node object:

$parent = $nodes->parentNode;

and done.

If you wonder about a certain string that PHP gives you and you have the feeling it might be something internal, you can quickly search all of PHP's codebase on http://lxr.php.net/, here is an example query for the string in your question.

Sign up to request clarification or add additional context in comments.

Comments

3

Just treat the xpath query nodes as filesystem paths, as mentioned here

Move up 2 nodes and get the parent and get what you need from it, such as the Id or Name.

Example:

<?php

$dom = new DOMDocument();
$dom->load("ProdCategories_small.xml");
$xpath = new DOMXPath($dom);

$parentNode = $xpath->query('//ProdCategory[Id="141"]/../..')->item(0);

$id = $xpath->query('./Id', $parentNode)->item(0);
$name = $xpath->query('./Name',$parentNode)->item(0);

print "Id: " . $id->nodeValue . PHP_EOL;
print "Name: " . $name->nodeValue . PHP_EOL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.