RE: [xml] XPath question



I think the operative thing here is, "Assuming foo is mapped to your
namespace name in the XPath context."  I probably should have mentioned
in my posting that I came up with my expression to deal with
environments where there was no mechanism to establish such mappings
other than by including a namespace prefix in the instance document.  As
far as I can see, XPath 1.0 does not specify such a mechanism, but
libxml does, of course.  So, my posting was not completely relevant to
this list.  I was excited to find a solution (even if inefficient) to a
real problem that I had; I hope I can be forgiven for choosing the wrong
forum to share it!


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

On Wed, Aug 04, 2004 at 11:45:29AM -0400, Jesse Pelton wrote:
So how do you solve the original problem?  That is, how do you 
construct an expression to select elements in a particular default
namespace?

  You should not care whether it is a default namespace or a normal
namespace. You build the XPath query using whatever prefix you want map
the prefix to the namespace name (i.e. the URI) in the XPath context and
run the query that way.
  As soon as you start selecting on the kind of prefix in the target
document there is something wrong, whether it's "foo", "bar" or the
default one should not matter from a semantic viewpoint.
  - Assuming foo is mapped to your namespace name "bar" in the XPath
context
  - assuming n is the name of the elements you're searching
  then
    //foo:n
  is way better than
    //*[local-name() = 'n' and namespace-uri() = 'bar']

  Now if you still want to only pick elements which are in the default
namespace you can still do
  - Assuming foo is mapped to your namespace name in the XPath context
  - assuming n is the name of the elements you're searching
  then
    //foo:n[name() = 'n']
 will do it, and will be quite cheaper than //* and then a selection.
the name() function retrieves the qname and name() = 'n' ensure there is
no prefix.

Daniel

-- 
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/



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