1

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>
4
  • Which online tools are you using? What are they returning? Commented Jan 26, 2013 at 2:08
  • Did you copy your XPath correctly? What is the "; at the end supposed to mean? Commented Jan 26, 2013 at 2:26
  • Please show real C# string - right now it contains non-escaped double-quotes... Commented Jan 26, 2013 at 2:47
  • The testers I've tried are: xpathtester.com/test & freeformatter.com/xpath-tester.html. I am escaping the " with \". The expected result is: <childnode1234> <identification>xyz</identification> </childnode1234> Commented Jan 26, 2013 at 2:58

2 Answers 2

4

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".

Sign up to request clarification or add additional context in comments.

1 Comment

@RossPatterson, Yes, there are a number of these that attract newbies -- only to find that they aren't compliant XPath implementations.
3

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']

1 Comment

+1 because I think //*[x and y] is stylistically preferable to //*[x][y], even though they're equivalent.

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.