-1

I have problem with Xpath function in PHP. I have created simpleXML than ran xpath to it. Then I wanted to run it to the result of the xpath but only apply it to first element. But it still taking all elements like there was no xpath used. I also printed how the array looked after first xpath and it took exacly what I wanted. Can anyone help, please?

$elem = simplexml_load_file($fileIn);

$xpathArg = "//".$arg['eFR'];
$prem = $elem->xpath($xpathArg);

$xpathArg2 = "//".$arg['eSE'];
print_r($prem[0]->xpath($xpathArg2));
2
  • 1
    please post a valid mini snippet of the XML in question. And what you want it to be. And what you got instead. And content of $arg[] - a complete, self-containing code example. Professional question = professional answers. Commented Mar 1, 2016 at 21:27
  • Possible duplicate of Foreach loop with xPath on SimpleXML object returning duplicate data Commented Mar 2, 2016 at 7:34

1 Answer 1

0

An XPath starting with / is an absolute path. You want a path starting from the context node, otherwise the context node is ignored. Simply prepending a . at the beginning of your second xpath query will ensure it starts from the node you specify.

$elem = simplexml_load_file($fileIn);

$xpathArg = "//".$arg['eFR'];
$prem = $elem->xpath($xpathArg);

$xpathArg2 = ".//".$arg['eSE'];
print_r($prem[0]->xpath($xpathArg2));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.