0

I have written a chunk of XML parsing which works successfully provided I use an absolute path.

I now need to take an XMLNode as an argument and run an xpath against this.

Does anyone know how to do this?

I tried using relative XPath queries without any success!!

Should it be this hard??

1
  • 1
    It's hard to help with such a generic question; could you post a sample xml file and xpath your trying to get working? Commented Sep 18, 2009 at 0:18

1 Answer 1

2

It would help to see examples of XPath expressions that don't work as you think they should. Here are some possible causes (mistakes I frequently make).

Assume an XML document such as:

<A> <B> <C d='e'/> </B> <C/> <D xmlns="http://foo"/> </A> 
  • forgetting to remove the top-level slash ('/') representing the document:

    document.XPathSelectElements("/A") // selects a single A node

    document.XPathSelectElements("//B") // selects a single B node

    document.XPathSelectElements("//C") // selects two C nodes

but

aNode.XPathSelectElements("/B") // selects nothing (this looks for a rootNode with name B) aNode.XPathSelectElements("B") // selects a B node bNode.XPathSelectElements("//C") // selects TWO C nodes - all descendants of the root node bNode.select(".//C") // selects one C node - all descendants of B 
  • forgetting namespaces.

    aNode.XPathSelectElements("D") // selects nothing (D is in a different namespace from A) aNode.XPathSelectElements("[local-name()='D' and namespace-uri()='http://foo']") // one D node

(This is often a problem when the root node carries a prefixless namespace - easy to miss)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.