Re: [xml] XPath question
- From: Daniel Veillard <veillard redhat com>
- To: Jesse Pelton <jsp PKC com>
- Cc: xml gnome org, Ed Day <eday obj-sys com>, Igor Zlatkovic <igor zlatkovic com>
- Subject: Re: [xml] XPath question
- Date: Wed, 4 Aug 2004 11:04:28 -0400
On Wed, Aug 04, 2004 at 10:55:33AM -0400, Jesse Pelton wrote:
I know the thread is quite old, but for a long time I fell for the
common wisdom that you can't use XPath to query for an element in a
default namespace, and having recently discovered that it ain't so, I
want to set the record straight.
The following expression uses the XPath namespace-uri() and local-name()
functions to select all <n> elements whose namespace is "another-ns"
(the second <n> element in Igor's example):
//*[namespace-uri()="another-ns" and local-name()="n"]
Igor's solution for selecting elements in a namespace (provide a
namespace prefix, apply it to each affected element, and use it in your
Xpath query) is commonly given. I think this is a bad idea (unless you
have complete control over the documents you process, and arguably even
then), because there's no guarantee that the prefix in your query exists
in any given document, or if it does, that it refers to the same
Then you still didn't understood how XPath and namespaces work together.
The XPath engine does *NOT* use the prefix to make the match but the
namespace URI associated to that prefix in the XPath context.
The namespace URI is authoritative, so it makes sense to
use it, as this expression does.
The only caveat is that I have no idea how efficiently an expression
like this is processed.
That solution is horribly inefficient It build a node list of
all element nodes in the document and then filter it with expensive
interpreted function call. Promoting that solution while you didn't
understood why Igor's one was accurate is not a good advice, really.
Daniel Veillard | Red Hat Desktop team http://redhat.com/
veillard redhat com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
] [Thread Prev