Hi, all.
when i run my code, i always got the xpath Invalid expression warning.
So here is my question:
Assume i have the xml file

<root>
        <items>
         <item id="1">
         <name>
            item1
         </name>
        </item>
         <item id="2">
         <name>
            item2
         </name>
        </item>
        </items>
</root>

And here is my php code

<?php
    $id = $_GET['id'];
    $xml = new SimpleXMLElement(file_get_contents('item.xml'));
    $item = $xml->xpath("/root/./.[@id='{$id}']");  //want to go to the item
    $items = $item->xpath("../..");//want to go to the items
 ?>

So I got the xpath invalid expression warnings and also for the go to the parent node part got the error. ???
Somebody could help me??
Thanks.

<?php    
  $id = $_GET['id'];
  $xml = new SimpleXMLElement(file_get_contents('item.xml'));    
  $item = $xml->xpath("/root/items/item[@id='{$id}']");
  print_r($item);
  echo '<hr/>';
  $items = $item[0]->xpath("../..");//want to go to the items 
  print_r($items);
?>

I've put print_r and a line in between, so you can see what happens. xpath returns an array, even though there is only one item. I do not think that a '.' is allowed in an xpath path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.