Retrieving a node list
Brought to you by:
bs_php,
nigelswinson
From: Eric A. <eri...@co...> - 2003-03-16 02:24:54
|
All my XPath hacking has previously been with Perl using XML::XPath, but I am working on a PHP project and I like using XPath retrieve config info in little XML files. I came across this project and thought that it looked pretty good, but I am having trouble figuring out how to do a XPath query and get back a list of nodes. So for example if I have a XML file as follows: <root> <tag>item1</tag> <tag>item2</tag> <tag>item3</tag> </root> I want to be able to say something like: $xPath = new XPath(); $xPath->importFromFile( $filename ); $list = $xPath->find( '/root/tag' ); foreach ($list as $item) { echo $item->getText() . '<br>'; } and have it output: item1 item2 item3 This is somewhat like the Perl syntax (mixed with a bit of your libraries). But things that are missing are a method that takes a XPathQuery and returns a list of nodes that match that query, and a method on the node that will return the text inside the node. I could probably achieve the second needed method by using the join function on 'textParts' attribute of the Node, but that seems a bit messy. Basically I am wondering what is the best way of reading a list of nodes that are returned from a XPathQuery. Everything in PHP.XPath seems to be based around giving an absolute XPathQuery or a requiring that only one node being returned by a query. How do you get a list of nodes that match a path? Thanks for your help and the great library. XPath greatly simplifies my code, and I will be real happy if I can use it in this PHP project. -- Eric Anderson There are 10 types of people in the word Those who can read binary, and those who can't |