php5: question about xpath

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Börni

    php5: question about xpath

    Hi,

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

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

    And then you do the following xpath query:

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

    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?
  • Janwillem Borleffs

    #2
    Re: question about xpath

    Börni wrote:[color=blue]
    > 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?[/color]

    <?php

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

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

    ?>


    JW



    Comment

    • Tim Van Wassenhove

      #3
      Re: php5: question about xpath

      On 2005-01-25, Börni <b.reiter@onlin ehome.de> wrote:[color=blue]
      > Suppose you have this xml file:[/color]

      [snip]
      [color=blue]
      > $nodes = $xpath->query("./news/*", $dom->documentElemen t);
      > 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?[/color]


      Have a look at the count function for xpath.

      --
      Met vriendelijke groeten,
      Tim Van Wassenhove <http://www.timvw.info>

      Comment

      Working...