... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2010-03-15

Java 6 XPath NamespaceContext Bug

In Java, one defines the namespaces for the XPath evaluator using the class NamespaceContext. In Java 6, the XPath evaluator does not consult the defined namespace context to interpret the default namespace. This despite explicit discussion of the default namespace in the Javadoc.

The workaround is to avoid using the default namespace in the XPath query.

It would appear that this somewhat surprising behaviour is consistent with the XPath 1.0 specification. Section 2.3 (Node Tests) states:

A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error if the QName has a prefix for which there is no namespace declaration in the expression context.

(emphasis is mine)

Blog Archive