Can XPath expressions use a default namespace?

A

Arndt Jonasson

The definition of XPath 1.0 says

"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."

As implementor of a program which uses XPath expressions to refer to
XML elements (the XPath expressions being sometimes embedded within
XML instance documents, sometimes supplied by other means), it seems
useful to me to let the program supply a default namespace (if
relevant) to the XPath expressions (*) and for XPath to be able to use
it for unprefixed names.

(*) or even let a larger environment than the XML instance document
determine what an unprefixed element means. For example if two
documents are visible, each wholly within its own namespace, with
top elements respectively "top1" and "top2", the expression
"/top1" would be unique without mentioning the namespace.

But the quoted text above seems to perhaps disallow this, forcing the
interpretation of an unprefixed name to imply the null namespace
instead of a defaulted one. Is that really the case?

(XSLT does enforce this, as does the program 'xmlstarlet', but this
of course does not prove anything one way or the other.)
 
M

Martin Honnen

Arndt said:
But the quoted text above seems to perhaps disallow this, forcing the
interpretation of an unprefixed name to imply the null namespace
instead of a defaulted one. Is that really the case?

Yes, with XPath 1.0 a name without a prefix selects an element or
attribute in no namespace.

With XPath 2.0 which is now a W3C recommendation things are different,
it introduces a default element/type namespace
<http://www.w3.org/TR/xpath20/#dt-def-elemtype-ns>
and that way a name without a prefix can select an element in that
default element namespace:
<http://www.w3.org/TR/xpath20/#node-tests>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top