This is a discussion on Possible using XPath? within the PHP General forums, part of the PHP Programming Forums category; Let's say I have the following structure: <?xml version="1.0" encoding="UTF-8"?&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Let's say I have the following structure:
<?xml version="1.0" encoding="UTF-8"?> <root> <child id="c1"> <child id="gc1"> <child id="ggc1"/> <child id="ggc2"/> </child> <child id="gc2"> <child id="ggc3"/> <child id="ggc4"/> </child> </child> <child id="c2"> <child id="gc3"> <child id="ggc5"/> <child id="ggc6"/> </child> <child id="gc4"> <child id="ggc7"/> <child id="ggc8"/> </child> </child> </root> By using the following XPath query //child[@id="gc3"]/child I can get the child nodes of "gc1". But what I'd really like to get is the sub branch/path going back to the root. So instead of just returning the two nodes <child id="ggc5"/> <child id="ggc6"/> I'd like to be able to return the sub branch/path <root> <child id="c2"> <child id="gc3"> <child id="ggc5"/> <child id="ggc6"/> </child> </child> </root> Is that possible? Or is this something I'd have to do programatically using the nodes returned by the XPath query? Basically, I'm just trying to get a fragment of the larger xml document... thnx, Christoph |
|
|||
|
On 27/03/2008, Christoph Boget <christoph.boget@gmail.com> wrote:
> Let's say I have the following structure: > > <?xml version="1.0" encoding="UTF-8"?> > <root> > <child id="c1"> > <child id="gc1"> > <child id="ggc1"/> > <child id="ggc2"/> > </child> > <child id="gc2"> > <child id="ggc3"/> > <child id="ggc4"/> > </child> > </child> > <child id="c2"> > <child id="gc3"> > <child id="ggc5"/> > <child id="ggc6"/> > </child> > <child id="gc4"> > <child id="ggc7"/> > <child id="ggc8"/> > </child> > </child> > </root> > > By using the following XPath query > > //child[@id="gc3"]/child > > I can get the child nodes of "gc1". But what I'd really like to get > is the sub branch/path going back to the root. So instead of just > returning the two nodes > > <child id="ggc5"/> > <child id="ggc6"/> > > I'd like to be able to return the sub branch/path > > <root> > <child id="c2"> > <child id="gc3"> > <child id="ggc5"/> > <child id="ggc6"/> > </child> > </child> > </root> > > Is that possible? Or is this something I'd have to do programatically > using the nodes returned by the XPath query? Basically, I'm just > trying to get a fragment of the larger xml document... //child[@id='gc3']/child/ancestor-or-self::* -robin |
|
|||
|
> > Is that possible? Or is this something I'd have to do programatically
> > using the nodes returned by the XPath query? Basically, I'm just > > trying to get a fragment of the larger xml document... > //child[@id='gc3']/child/ancestor-or-self::* Thanks for the response. However, I must be doing something wrong here. The test script below isn't doing what I'm expecting: $xml = '<?xml version="1.0" encoding="UTF-8"?><root><child id="c1"><child id="gc1"><child id="ggc1">Great Grand Child 1</child><child id="ggc2">Great Grand Child 2</child></child><child id="gc2"><child id="ggc3">Great Grand Child 3</child><child id="ggc4">Great Grand Child 4</child></child></child><child id="c2"><child id="gc3"><child id="ggc5">Great Grand Child 5</child><child id="ggc6">Great Grand Child 6</child></child><child id="gc4"><child id="ggc7">Great Grand Child 7</child><child id="ggc8">Great Grand Child 8</child></child></child></root>'; $doc = new DOMDocument('1.0', 'UTF-8'); $doc->loadXML( $xml ); $xpath = new DOMXPath($doc); $nodeList = $xpath->query("//child[@id='gc3']/child/ancestor-or-self::*"); echo 'Got list list of [' . $nodeList->length . '] nodes:<br>'; for ($i = 0; $i < $nodeList->length; $i++) { echo $nodeList->item($i)->nodeValue . "<br>\n"; } When I run the XPath query through XMLSpy, the correct nodes are returning. However, when I run it through DOMXPath->query() (or evaluate() for that matter), it doesn't seem to be returning the correct nodes. What's going wrong? thnx, Chris |
|
|||
|
On 27/03/2008, Christoph Boget <jcboget@yahoo.com> wrote:
> > > Is that possible? Or is this something I'd have to do programatically > > > using the nodes returned by the XPath query? Basically, I'm just > > > trying to get a fragment of the larger xml document... > > //child[@id='gc3']/child/ancestor-or-self::* > > > Thanks for the response. However, I must be doing something wrong > here. The test script below isn't doing what I'm expecting: > > $xml = '<?xml version="1.0" encoding="UTF-8"?><root><child > id="c1"><child id="gc1"><child id="ggc1">Great Grand Child > 1</child><child id="ggc2">Great Grand Child 2</child></child><child > id="gc2"><child id="ggc3">Great Grand Child 3</child><child > id="ggc4">Great Grand Child 4</child></child></child><child > id="c2"><child id="gc3"><child id="ggc5">Great Grand Child > 5</child><child id="ggc6">Great Grand Child 6</child></child><child > id="gc4"><child id="ggc7">Great Grand Child 7</child><child > id="ggc8">Great Grand Child 8</child></child></child></root>'; > > $doc = new DOMDocument('1.0', 'UTF-8'); > $doc->loadXML( $xml ); > > $xpath = new DOMXPath($doc); > $nodeList = $xpath->query("//child[@id='gc3']/child/ancestor-or-self::*"); > > echo 'Got list list of [' . $nodeList->length . '] nodes:<br>'; > for ($i = 0; $i < $nodeList->length; $i++) { > echo $nodeList->item($i)->nodeValue . "<br>\n"; > } > Only the nodes specified are in the list, but the *values* of the those nodes include children that aren't in the list. change your for-loop to this and you'll see just the expected nodes: foreach ($nodeList as $node) { echo $node->tagName, ' : ', $node->getAttribute('id'), "<br>\n"; } does that make sense? -robin |