0

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.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by pritaeas
0
<?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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.