[jdom-interest] Two namespace-questions (revisited)

Kai Wörner herrner at gmail.com
Wed Jun 6 07:35:57 PDT 2007

Hello again,

I'm stumbling over this same problem again and again! Now I'd like to
simply show parts of the same input XML file as last time inside a
Java program based on an XPath-expression. I simply provide an input
field for the XPath-string and seeing the problems that the users
already have drafting a XPath-expression, I simply _know_ that they'll
fail if they have to add a namespace-declaration before each and every
element-name they use (especially since they don't have to do this in
any of the XML-Editors they use).
In addition, if I use an XMLOutputter to output the matched Elements,
they come out with another load of namespace-bulk none of my users
will understand. So in my tool I'd have to use an expression like:


to get a result that looks like that:

<s xmlns="http://www.tei-c.org/ns/1.0" part="N" TEIform="s">
                        <cl type="lMAI tDEC" part="N" TEIform="cl">

as opposed to any XML-Editor where I'd use:


to get:

                    <cl type="lMAI tDEC">

Is it possible /at all/ to mimic the way it works in a XML-Editor at
all in JDom or do I have to look someplace else?

Thanks again,


XML: http://files.exmaralda.org/bl.xml

> There is no default namespace in XPath 1.0.
> So you have to use a namespace prefix in your XPath expressions. You can
> choose any prefix as there is no relationship between the prefixes used in
> XPath and the ones in the document: prefixes are just shortcuts and the
> matching is done on the actual namespace URIs.
> The following should work:
> XPath xp = XPath.newInstance("/x:*");
> xp.addNamespace("x", "http://www.tei-c.org/ns/1.0");
> xp.selectNodes(document);

More information about the jdom-interest mailing list