Re: Possible using XPath?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 27/03/2008, Christoph Boget <jcboget@xxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux