kevinc
kevinc

Reputation: 636

XPath query works with online tools but not in .NET

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

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

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

Anton Tykhyy
Anton Tykhyy

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

Related Questions