Schultz9999
Schultz9999

Reputation: 8926

dot vs. slash in XPath expression

I came across an unusual problem. I have this XML:

<T durationMs="400">
  <foo durationMs="407">
    <foo-child durationMs="307" />
  </foo>
  <bar durationMs="208">
    <bar-child durationMs="108" />
  </bar>
</T>

I am using XPathExtentions to get an XElement out of this XML:

var xe = XElement.Parse(s);
var foo = xe.XPathSelectElement("/T/foo")

It returns nothing. However if I use:

var foo = xe.XPathSelectElement("./foo")

It gets an elements. So what's the difference between dot and slash in this case?

Upvotes: 1

Views: 3537

Answers (2)

Anirudha
Anirudha

Reputation: 32797

/ selects from the root node.

So with /T/foo it's trying to match T->T->foo which for sure won't match

. depicts the current node in which case it would be the root node

/foo would work

Upvotes: 4

Parimal Raj
Parimal Raj

Reputation: 20575

. selects the current node.

/ Selects from the root node.

// Selects nodes in the document from the current node that match the selection no matter where they are.

XPath Syntax gives you a brief idea of how selections are done


In your case ./foo denotes selection from root node i.e. T

Upvotes: 2

Related Questions