Hi Jon

The problem is that there is no prefix used in the document. If your
document did define a prefix

<Schema name="whatever"
        xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:x="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes">

    <foo attr = "1"/>

</Schema>

Then you'd be able to use it as

//x:foo/@attr

Since dom4j will resolve any prefixes used in your XPath expression with the
root node on which you evaluate XPaths (unless you specify your own
NamespaceContext object to do this resolution).

Though in XPath its not possible to define the empty prefix to map to some
namespace URI. So //foo will *always* only match foo elements that are not
in any particular namespace URI. From XPath's perspective, the only way
around this is to

(i) use a prefix in your XPath expression
(ii) use the ugly local-name()='Schema' notation.

Note that if you use a prefix in your XPath expression you can still happily
query documents that don't use a prefix, or that use a different prefix. The
only thing that matters is the namespace URIs that all these prefixes map
to.

James
----- Original Message -----
From: "Jonathan Locke" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 30, 2002 10:46 PM
Subject: [dom4j-user] xpath with unnamed default namespace



i've seen various comments on the net regarding xpath's inability to select
nodes
from a document with an unnamed default namespace.  this leads to some
pretty non-intuitive behaviour.

a document like:

<Schema name="whatever"
        xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes">

    <foo attr = "1"/>

</Schema>

cannot be queried with an expression like "//Schema/foo@attr" because the
first
namespace has no explicit name, it is kindof invisible to xpath.  one can
kludge
around this with something like "*//[name()='Schema']/foo@attr", but that's
just
wrong!  ;-)

have seen a couple of hacks involving calling setNamespaceURIs, but have
been
unable to get this to work right away, so i figured posting might turn up a
better
solution...

anyway, someone out there was commenting on a general solution involving
setting
the URIs in the document's namespace context *automatically* by creating a
new
document factory.  this would be *really* nice because i don't explicitly
create any
XPath objects (i use the convenience methods all over the place instead!).
does
anyone have code that does this (or perhaps a brief description of how to do
it)?

thanks!!

     jon



-------------------------------------------------------
This sf.net email is sponsored by: Dice - The leading online job board
for high-tech professionals. Search and apply for tech jobs today!
http://seeker.dice.com/seeker.epl?rel_code1
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user


__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com


-------------------------------------------------------
This sf.net email is sponsored by: Dice - The leading online job board
for high-tech professionals. Search and apply for tech jobs today!
http://seeker.dice.com/seeker.epl?rel_code=31
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to