RE: [xml] XPath question

So how do you solve the original problem?  That is, how do you construct
an expression to select elements in a particular default namespace? 

-----Original Message-----
From: Daniel Veillard [mailto:veillard redhat com] 
Sent: Wednesday, August 04, 2004 11:04 AM
To: Jesse Pelton
Cc: Igor Zlatkovic; Ed Day; xml gnome org
Subject: Re: [xml] XPath question

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
(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 namespace URI.

  Then you still didn't understood how XPath and namespaces work
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
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]