Possible using XPath?

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"?&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-27-2008
Christoph Boget
 
Posts: n/a
Default Possible using XPath?

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
Reply With Quote
  #2 (permalink)  
Old 03-27-2008
Robin Vickery
 
Posts: n/a
Default Re: [PHP] Possible using XPath?

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
Reply With Quote
  #3 (permalink)  
Old 03-27-2008
Christoph Boget
 
Posts: n/a
Default Re: [PHP] Possible using XPath?

> > 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
Reply With Quote
  #4 (permalink)  
Old 03-27-2008
Robin Vickery
 
Posts: n/a
Default Re: [PHP] Possible using XPath?

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:35 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0