xpath expression from xml with namespace prefix

Chaitanya picture Chaitanya · Oct 14, 2010 · Viewed 42.3k times · Source

I could not get the following xpath expression to work when the xml path namespace prefix set.

/bk:BookStore/bk:Books/bk:Book[text()='Time Machine']

XML is:

<BookStore xmlns:bk="http://www.bookstore.com/book#">
  <bk:Books>
    <bk:Book id="1">Time Machine></bk:Book>
  </bk:Books>
</bk:BookStore>

Answer

Ivivi Vavava picture Ivivi Vavava · Nov 18, 2011

Or even better (and more portable), without the unnecessary prefix:

/*/*[local-name()='Books'] ... and so on

The function local-name ignores any prefix, which, as correctly stated by commenters, can vary.