Topic
  • 2 replies
  • Latest Post - ‏2009-10-02T00:42:45Z by SystemAdmin
SystemAdmin
SystemAdmin
746 Posts

Pinned topic Retreiving parents...

‏2009-10-01T09:26:04Z |
Hello, this is my first post here. Thank you in advance.
I think I have a simple question:

This is my XML (example):

<world>
<country id="100" name="Japan">
<state id="25" name="Aichi">
<city id="202" name="Nagoya" />
<city id="203" name="Toyohashi" />
...
</state>
<state id="26" name="Mie">
<city id="202" name="Mie" />
...
</state>
...
</country>
...
</world>

(... means: continue with similar data)

I'm trying to retrieve all cities (using PHP) like:

$xpath = new DOMXPath($doc);
$xpath->xquery("/world/country/state@id=25");

And I receive a list like:

<city id="202" name="Nagoya" />
<city id="203" name="Toyohashi" />

The problem is that I don't receive any information from the parents inside the nodes.
So, I would like to obtain the nodes with the parental relationship like:

<world>
<country id="100" name="Japan">
<state id="25" name="Aichi">
<city id="202" name="Nagoya" />
<city id="203" name="Toyohashi" />
...
</state>
</country>
</world>

Can this be achieved using only Xpath?

Thank you.
Updated on 2009-10-02T00:42:45Z at 2009-10-02T00:42:45Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Retreiving parents...

    ‏2009-10-01T22:17:02Z  
    I don't know the PHP interface to XPath, but if it's like most other XPath APIs, then it actually returns references to the Node objects selected by the XPath expression. It might be that when you convert these to strings or otherwise display them, they are shown with the children but not the parents; but the actual Node object returned to your application will have methods to get the parent just as easily as you can get the children..

    You can't construct new documents or nodes using XPath, so you can't retrieve the result you are looking for as a piece of XML. For that you would need XSLT or XQuery
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Retreiving parents...

    ‏2009-10-02T00:42:45Z  
    I don't know the PHP interface to XPath, but if it's like most other XPath APIs, then it actually returns references to the Node objects selected by the XPath expression. It might be that when you convert these to strings or otherwise display them, they are shown with the children but not the parents; but the actual Node object returned to your application will have methods to get the parent just as easily as you can get the children..

    You can't construct new documents or nodes using XPath, so you can't retrieve the result you are looking for as a piece of XML. For that you would need XSLT or XQuery
    Thank you MicahelKay,

    Yes, actually I'm using XQuery to retrieve the results:
    
    $nodes = $xpath->xquery(
    "/world/country/state[@id=25]");
    

    The result is a NodeList in which I can extract each of the nodes.
    And then export them as XML.
    So I got it work searching for the parents (as you said), before exporting the XML
    with $node->parentNode, as usual. I thought the solution was in the Xpath... but no...

    Thank you!