Firstly both documents should be using a well defined namespace. If thats
the case then the prefix doesn't matter - you can just bind whatever prefix
you want to the expected namespace (e.g. SOAP's namespace URI) then use your
own prefix in XPath - irrespective of the prefixes used in the document.

e.g.

// register prefixes with my factory
Map uris = new HashMap();
uris.put( "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"; );

DocumentFactory factory = new DocumentFactory();
factory.setXPathNamespaceURIs( uris );

// now parse a document using my factory
SAXReader reader = new SAXReader();
reader.setDocumentFactory( factory );
Document doc = reader.read( "soap.xml" );

// now lets use the prefixes
Node element = doc.selectSingleNode( "/SOAP-ENV:Envelope/SOAP-ENV:Body" );



Or if the prefix or namespace URIs are unknown, then you could use the XPath
expression...

*[local-name()='Body']

Which is a bit ugly but then its rare to not care about the namespace URI.

James
----- Original Message -----
From: "Brain, Jim" <[EMAIL PROTECTED]>
To: "'James Strachan'" <[EMAIL PROTECTED]>; "David Hooker"
<[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, March 05, 2002 4:08 PM
Subject: RE: [dom4j-user] default namespace issue


> How do you do this if you don't know the prefixes?
>
> Say I get two XML files:
>
> #1
>
> <u:Body>
> .
> .
> .
> .
> </u:Body>
>
> #2:
>
> <a:Body>
> .
> .
> .
> </a:Body>
>
> u and a are mapped to same namespace.
>
> I want to select Body.
>
> How can I do so in a way that is not so dependent on what prefix the
> original author of the document used?
>
> Jim
>
>
> Jim Brain, [EMAIL PROTECTED]
> "Researching tomorrow's decisions today."
> (319) 369-2070 (work)
> SYSTEMS ARCHITECT, ITS, AEGON FINANCIAL PARTNERS
>
>  -----Original Message-----
> From: James Strachan [mailto:[EMAIL PROTECTED]]
> Sent: Monday, March 04, 2002 8:55 PM
> To: David Hooker; [EMAIL PROTECTED]
> Subject: Re: [dom4j-user] default namespace issue
>
> From: "David Hooker" <[EMAIL PROTECTED]>
> > Isn't there a dom4j API call to get the namespace URI's from the XML
> > file and bind them into your xpath expression?
>
> Kind of, though a document can redefine prefixes <-> URI mappings
throughout
> the document, so you need to pick a node and get the namespaces available
> there.
>
> By default dom4j with inherit any namespace prefixes -> URIs that are
> available in the source node on which XPaths occur. e.g. if you parsed
this
> document
>
> <foo:root
>     xmlns:foo="http://www.foo.com/123";
>     xmlns:bar="http://somewhere.com/bar";>
>     <bar:one>
>         <bar:two>hello!</bar:two>
>     </bar:one>
> </foo:root>
>
> Then the following XPath expression on the document (or
> document.getRootElement()) should work fine...
>
> foo:root/bar:one/bar:two
>
> James
>
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to