[jdom-interest] Namespaces

bob bob at accurev.com
Mon Aug 28 22:24:28 PDT 2000


Howdy Again--

It seems that JDOM doesn't keep declared-but-unused namespace
prefixes handy.

JDOM seems to only keep up with NS that are actually used for elements
or attributes, and discards (?) or at least makes unreachable, any
not explicitly used in that manner.

In XSLT, the namespace prefix can be used by an XPath which is
contained between quotes as the value of an attribute.

The prefixes valid in an xpath expression are all declared
prefixes up to the element which contains the expression.

You might have a situation like this:

  <a xmlns="http://foo.org/"
     xmlns:bar="http://bar.org/"
     xmlns:fnord="http://fnord.org/"
     size="9" name="cheese burgers">
          <bar:b>
                  <c expr="//fnord:one">goober
                          <d name="one" size="toast"></d>
                  </c>
          </bar:b>
          <b>
                  <c name="two">snort
                  </c>
          </b>
  </a>

The xmlns:fnord declaration isn't actually used in the
structure of the document, but does define fnord: to be
a valid prefix up to that point.  In <c>, it's 'expr'
attribute contains an XPath that needs to be able to resolve
fnord: into the namespaceURI.

When you turn around, and toss the document back to the
XMLOutputter, we lose the xmlns:fnord declaration, changing
the document's semantics (and in fact, making it an illegal
document for XPath/XSLT purposes).

  <?xml version="1.0" encoding="UTF-8"?>
  <a xmlns="http://foo.org/" size="9" name="cheese burgers">

            <bar:b xmlns:bar="http://bar.org/">

                      <c expr="//fnord:one">goober
                              <d name="one" size="toast" />

                      </c>

            </bar:b>

            <b>

                      <c name="two">snort
                  </c>

            </b>
  </a>

Ideally, a method on Element would be sufficient, to translate
prefix to namespaceURI based upon all declarations on the stack
thus far.  Or, just return the Namespace object.

ie.

public class Element
{
	public Namespace getNamespace(String prefix)
	{ ... }
}

I'm going to hack my copy, to attempt to support this, maybe,
and if sucessful, I'll submit a patch.

	-bob




More information about the jdom-interest mailing list