php5: question about xpath

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I couldnt find an answer to my question in the php manual, so i try it
Basically i just want to know if the following is possible.
Suppose you have this xml file:

   <news id="1">
     <message>Just a test</message>
   <news id="2">
     <title>Test 2</title>
     <message>yet another test</message>

And then you do the following xpath query:

$nodes = $xpath->query("./news/*", $dom->documentElement);

Now, how can i tell how many childs news has? Because $nodes has 4
elements, so one news element could have 4 childs or there are 2 news
elements with 2 childs...
Is there a solution?

Re: php5: question about xpath

Quoted text here. Click to load it


Quoted text here. Click to load it

Have a look at the count function for xpath.

Met vriendelijke groeten,
Tim Van Wassenhove <

Re: question about xpath

Brni wrote:
Quoted text here. Click to load it


// Get the context nodes
$nodes = $xsl->query("//root/news");
$pos = 0;

while (++$pos < ($nodes->length + 1)) {
     $children = $xsl->query(
          $nodes->item($pos - 1)
     print "//news[$pos] has ";
     print $children->length;
     print " child elements<br />";



Site Timeline