Post details: xpath and namespaces

Aug 31, 2005 : xpath and namespaces

After a lot of googling, I finally found how to get xpath and xml namespaces to work together. The way I ended up doing it was this to duplicate the default namespace and reference it everywhere.

XML file:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" ...>
...
</web-app>

XPath:
//j2ee:web-app/j2ee:servlet[j2ee:servlet-name='myapp']

Comments, Pingbacks:

Comment from: Brian Ewins [Visitor] · http://jaxen.org/
I think you're getting overly hung up on the prefixes.

[web-app xmlns="http://java.sun.com/xml/ns/j2ee"...]
...
[/web-app]

with the xpath:
//foo:web-app/foo:servlet[foo:servlet-name='myapp']

will work too as long as foo is bound to the namespace "http://java.sun.com/xml/ns/j2ee" in your xpath processor. Its unclear what the context of the xpath is you struggled with, but in xsl, you can use this like:

[xsl:stylesheet xmlns:xsl="..." xmlns:foo="http://java.sun.com/xml/ns/j2ee"]
[xsl:template match="//foo:web-app/foo:servlet[foo:servlet-name='myapp']"]...[/xsl:template]
[/xsl:stylesheet]

remember prefixes are just aliases, and prefixes in the stylesheet are nothing to do with those in the processed document.

In xpath libraries (eg jaxen) theres usually some other way of binding prefixes to namespaces, but those prefixes are only relevant in the xpath itself.
Permalink 08/31/05 @ 11:51
Comment from: Dave Brondsema [Member] · http://brondsema.net/
Thanks for the additional info. I am using the ant xmltask from http://www.oopsconsultancy.com/software/xmltask/ and it does not appear to have any support for configuring the xpath processor it uses.
Permalink 08/31/05 @ 12:35

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.
What is the 2-letter abbreviation for Michigan?
Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

<  June 2019  >
Mon Tue Wed Thu Fri Sat Sun
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Categories


Archives

Misc

Syndicate this blog XML

powered by
b2evolution