xpath query – filtering by node content is not working [solved]

Let’s say you have the following xml:

<?xml version=”1.0″ ?>
<user>
<firstname>MyName</firstname>
</user>

If you want to select the firstname node containing the value “MyName”, do NOT proceed as follow:

$xpath = '/user/firstname = "MyName"';

But rather do:

$xpath = ‘/user/firstname[. = “MyName”]’;

Similarly, let’s say you want to return <user> node instead of <firstname> node, now your xpath query is simply:

$xpath = '/user[firstname = "MyName"]';

Note as we wrote [firstname = “xxx”] and NOT [/firstname = “xxx”]. In this case, when we add the forward slash, we test root node <firstname> and not the node within our current node (i.e. <user> node)

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...