Hi James,
You are right about the confusing aspects of namespaces, esp. regarding XPath expressions. The standard states that namespace prefixes in XPath expressions are resolved with regard to the "expression context", which I presume is what part of the tree is currently under examination.
In the following example XML document, the prefix z is mapped to three different namespaces on three different elements:
<?xml version="1.0"?>
<z:A xmlns:z="urn:zorglub">
<z:B xmlns:z="urn:zappa">
<z:C xmlns:z="urn:zoot">
</z:C>
</z:B>
</z:A>
If I do selectNodes("//z:*") on the document node I get both z:A, z:B and z:C back. This would indicate to me that the XPath namespace prefixes used are not "totally independant of the prefixes used in the source document", as you state, but I guess this would depend on exactly what is meant by the "expression context".
Of course, your suggestion should still work since it establishes a NamespaceContext explicitly.
Any XPath gurus care to comment?
Cheers,
-- Steen
-----Original Message-----
From: James Strachan [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, September 26, 2001 1:07 AM
To: Steen Lehmann - SilverStream; 'Martijn Koster'
Cc: [EMAIL PROTECTED]
Subject: Re: [dom4j-user] simplest way to use namespaces URI in xpath in dom4j?
Hi Guys
One other option is to explicity setup an org.jaxen.NamespaceContext object with one or more namespace prefixes matched to the namespace URIs you need which can then be used in an XPath.
I'm thinking of adding something like the following to the XPath interface (along with maybe similar changes in Jaxen)
Document doc = ..;
// create the XPath with our namespace prefixes
XPath xpath = doc.createXPath( "/foo:aaa/foo:bbb/foo:xxx" );
// map the prefixes to the URIs we want
xpath.addNamespace( "foo", "http://www.example.org/my/" );
// now lets use it
List list = xpath.selectNodes( doc );
A word of warning though - namespaces seem to be an area of much confusion - but the namespace prefixes that are mapped to URIs inside an XPath expressoin (such as the above xpath object) are totally independent of the prefixes used in the source document. To put that another way, it doesn't matter what prefixes you choose to use in your XPath expressions, only what URIs they map to and the namespace URIs that are in the source document.
James
----- Original Message -----
From: Steen Lehmann - SilverStream
To: 'Martijn Koster'
Cc: '[EMAIL PROTECTED]'
Sent: Tuesday, September 25, 2001 7:24 AM
Subject: RE: [dom4j-user] simplest way to use namespaces URI in xpath in dom4j?
This is a way to match on a known namespace uri and local element name. It's a bit on the verbose side.
/*[local-name()="aaa" and namespace-uri()="http://www.example.org/my/"]/*[local-name()="bbb" and namespace-uri()="http://www.example.org/my/"]
If you know that no other elements are called aaa or bbb in the document regardless of namespaces, you can omit the test for namespace-uri in the expression and end up with:
/*[local-name()="aaa"]/*[local-name()="bbb"]
which is a bit nicer.
BTW you will want to use single quotes instead of double quotes (or escape the double quotes) to embed the expressions in a Java String.
-- Steen
> -----Original Message-----
> From: Martijn Koster [mailto:[EMAIL PROTECTED]]
> Sent: Monday, September 24, 2001 6:16 PM
> To: [EMAIL PROTECTED]
> Subject: [dom4j-user] simplest way to use namespaces URI in xpath in
> dom4j?
>
>
> What is the easiest way in Dom4J to specify an XPath expression for a
> document where you know the namespace URI of the desired nodes, but
> not the prefix? Ie in:
>
> <?xml version='1.0'?>
> <my:aaa xmlns:my="http://www.example.org/my/"
> xmlns:his="http://www.example.org/his/">
> <my:bbb>
> <my:xxx/>
> <his:xxx/>
> </my:bbb>
> </my:aaa>
>
> I want to select element my:xxx, but I don't know the prefix "my",
> only "http://www.example.org/my/". I can navigate to it using QNames,
> but I want to use a single XPath expression. I can do it by hand:
>
> Namespace ns =
> root.getNamespaceForURI("http://www.example.org/my/");
> String prefix = ns.getPrefix();
> doc.selectNodes("/" + prefix ":aaa/" + prefix + ":bbb/" +
> prefix + ":xxx");
>
> but that gets ugly. Is there a way I can do something like:
>
> doc.selectNodes("/aaa/bbb/xxx", ns);
>
> or
>
> doc.selectNodes(new XPathExpression("/aaa/bbb/xxx", ns));
>
> or some such?
>
> -- Martijn
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>
