Reputation: 636
I am able to get the below xpath to work against the below xml with online xpath tools but I am getting an "Expression must evaluate to a node-set." exception in .NET 4.5
xpath:
//*[starts-with(name(.), "childnode")[identification/text()='xyz']]
xml:
<rootnode>
<childnode1234>
<identification>xyz</identification>
</childnode1234>
<childnode3456>
<identification>abc</identification>
</childnode3456>
</rootnode>
The expected result is
<childnode1234>
<identification>xyz</identification>
</childnode1234>
Upvotes: 1
Views: 549
Reputation: 243459
Just change:
//*[starts-with(name(.), "childnode")[identification/text()='xyz']]
to:
//*[starts-with(name(.), "childnode")][identification/text()='xyz']
I recommend to avoid untested and obviously buggy "no name" "online xpath tools".
Upvotes: 4
Reputation: 20056
The online implementations are too relaxed. Microsoft XPath is right: starts-with()
evaluates to a boolean, not to a node-set. Try
//*[starts-with(name(.), 'childnode') and identification/text()='xyz']
Upvotes: 3