Title: RE: [dom4j-user] XPath w/ Namespace

Igor,

As Steen pointed out there was an error in the XPath expression I wrote in my last reply.  See his post for the correct expression.  I guess an other alternative would be to query the Document for the namespace prefix used in the Document.

Document d = ...;
String fooNs = d.getRootElement().getNamespaceForURI("http://www.dom4j.org").getPrefix();
String xpath = '/' + fooNs + ":aaa/" + fooNs + ":bbb";

But that's kind of ugly too.

I don't see an easy way to add a namespace to the Document.  The namespace specification doesn't allow an element to declare two namespace mappings for the same URI (unless one of them is the default namespace).  So unless you of course know that the root element doesn't define a namespace mapping for the URI, this won't work.

--
knut

> -----Original Message-----
> From: Igor Akkerman [mailto:[EMAIL PROTECTED]]
> Sent: Dienstag, 25. Februar 2003 11:04
> To: [EMAIL PROTECTED]
> Subject: RE: [dom4j-user] XPath w/ Namespace
>
>
> Thanks for your help, Knut. I already thought about using the XPath
> interface. But this requires much code rewriting for me and
> my partners.
>
> Could there be some other simple way like adding a new
> namespace declaration
> to the document and using it?
>
> Igor
>
> -----Original Message-----
> From: Wannheden, Knut [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, February 25, 2003 9:14 AM
> To: [EMAIL PROTECTED]
> Cc: 'Igor Akkerman'
> Subject: RE: [dom4j-user] XPath w/ Namespace
>
>
> By default, IIRC, the namespace mappings used in the XPath
> expression are
> the same as those in the Document you run the query against. 
> But using
> plain XPath you could do something like this):
>         /aaa[namespace-uri(.) =
> 'http://www.dom4j.org']/bbb[namespace-uri(.)
> = 'http://www.dom4j.org']
> alternatively (using the dom4j api) you could also do
> something like this:
> XPath xp =
> DocumentFactory.getInstance().createXPath("/foo:aaa/foo:bbb");
> Map nsMap = new HashMap();
> nsMap.put("foo", "http://www.dom4j.org");
> xp.setNamespaceURIs(nsMap);
> xp.selectNodes(yourContextNode);
> Hope this helps,
> --
> knut
> > -----Original Message-----
> > From: Igor Akkerman [mailto:[EMAIL PROTECTED]]
> > Sent: Dienstag, 25. Februar 2003 07:52
> > To: [EMAIL PROTECTED]
> > Subject: [dom4j-user] XPath w/ Namespace
> >
> >
> > Hi,
> >
> > how do I address an element with an xpath that has a
> > namespace not knowing
> > what prefix is set for the namespace?
> >
> > Here is an example document:
> > <nsa:aaa
> > xmlns:nsa="http://www.dom4j.org"><nsa:bbb>foo</nsa:bbb></nsa:aaa>
> >
> > I used the xpath expression "/nsa:aaa/nsa:bbb" to get the
> > nsa:bbb element.
> > But what if the document looks like this?
> > <nsb:aaa
> > xmlns:nsb="http://www.dom4j.org"><nsb:bbb>foo</nsb:bbb></nsb:aaa>
> >
> > The second document is absolutely equivalent to the first since the
> > namespace prefix is only symbolic for the url which is
> equal for both
> > documents. Also the following document is equivalent:
> > <aaa xmlns="http://www.dom4j.org"><bbb>foo</bbb></aaa>
> >
> > How can I ignore the prefix for addressing elements? (The
> document may
> > contain two or three different namespaces, so I cannot only
> > use the local
> > name!)
> >
> > Igor
> >
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net email is sponsored by:ThinkGeek
> > Welcome to geek heaven.
> > http://thinkgeek.com/sf
> > _______________________________________________
> > dom4j-user mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/dom4j-user
> >
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>

Reply via email to