[PHP] 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 //[EMAIL PROTECTED]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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Possible using XPath?
On 27/03/2008, Christoph Boget [EMAIL PROTECTED] 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 //[EMAIL PROTECTED]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... //[EMAIL PROTECTED]'gc3']/child/ancestor-or-self::* -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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... //[EMAIL PROTECTED]'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?rootchild id=c1child id=gc1child id=ggc1Great Grand Child 1/childchild id=ggc2Great Grand Child 2/child/childchild id=gc2child id=ggc3Great Grand Child 3/childchild id=ggc4Great Grand Child 4/child/child/childchild id=c2child id=gc3child id=ggc5Great Grand Child 5/childchild id=ggc6Great Grand Child 6/child/childchild id=gc4child id=ggc7Great Grand Child 7/childchild id=ggc8Great Grand Child 8/child/child/child/root'; $doc = new DOMDocument('1.0', 'UTF-8'); $doc-loadXML( $xml ); $xpath = new DOMXPath($doc); $nodeList = $xpath-query(//[EMAIL PROTECTED]'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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Possible using XPath?
On 27/03/2008, Christoph Boget [EMAIL PROTECTED] 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... //[EMAIL PROTECTED]'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?rootchild id=c1child id=gc1child id=ggc1Great Grand Child 1/childchild id=ggc2Great Grand Child 2/child/childchild id=gc2child id=ggc3Great Grand Child 3/childchild id=ggc4Great Grand Child 4/child/child/childchild id=c2child id=gc3child id=ggc5Great Grand Child 5/childchild id=ggc6Great Grand Child 6/child/childchild id=gc4child id=ggc7Great Grand Child 7/childchild id=ggc8Great Grand Child 8/child/child/child/root'; $doc = new DOMDocument('1.0', 'UTF-8'); $doc-loadXML( $xml ); $xpath = new DOMXPath($doc); $nodeList = $xpath-query(//[EMAIL PROTECTED]'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