RE: [XPath] namespaces

> 
> This may be covered or obviated by things already in the XPath 2.0 WD 
> (I'm not terribly good at reading the specification documents 
> admittedly), but I'd like to bring up some issues with namespaced 
> elements--from the perspective of XPath 1.0--in the hopes 
> that they'll 
> be addressed in XPath 2.0. Namely, that namespaced elements are 
> difficult and clunky to access and it'd be nice to have an easy 
> canonical way of accessing them.

XPath 2.0 introduces a "default namespace for elements" as part of the
static context. It's up to the host language how this is set: for example,
an API that allows XPath to be invoked from a Java application might allow
it to be set using the API. In XSLT 2.0 it is set using the
xpath-default-namespace attribute on the xsl:stylesheet or any other
enclosing element. In XQuery 1.0 it is set using "declare default namespace"
in the query prolog, or using xmlns="uri" on any enclosing direct element
constructor. The default namespace for elements is applied to any unprefixed
element name appearing in a path expression.
> 
> Let us take for example an XML document that contains some XHTML. All 
> XHTML tags are placed under the "html" namespace in order to 
> easily sort 
> them out from the rest of the document. If we have some <html:p> 
> elements under the <foo> element, to be accessed they'd need to have 
> XPaths like "/foo/child::*[name() = 'html:p']" or if one were being 
> technically correct, "foo/child::*[local-name() = 'p' and 
> namespace-uri() = 'http://www.w3.org/1999/xhtml']". Neither 
> one of these 
> is really simple or accessible.
> 
> I'd be nice to have XPaths like "foo/html:p" work.

But they do! So long as there is a namespace binding in the static context
that binds the html prefix, this works both for XPath 1.0 and XPath 2.0.

Michael Kay
http://www.saxonica.com/

Received on Sunday, 10 October 2004 11:43:25 UTC