2

Is it possible to bind xml prefix→namespace mapping to a map using javax.xml.bind.* annotations?

Something like:

class FooPOJO {
...
  @Namespacebindingannotation
  Map<String,String> prefixToNamespaceMap;
...
}

And in the xml

<foo
  xmlns="http://foo.com"
  xmlns:bar="http://bar.com"
  xmlns:baz="http://baz.com">
...

When unmarshaling the xml, prefixToNamespaceMap will map the 3 prefixes to their appropriate namespaces.

eladidan
  • 2,634
  • 2
  • 26
  • 39

1 Answers1

1

You want to dynamically extract the namespaces URIs and the prefixes from the XML? That's a genuinely bad idea.

XML namespaces are part of the contract between your application code and the XML it processes. They ought to be hard-coded into your application.

The reason is simple. These XMLs look different but are all the same document:

<foo:root xmlns:foo="http://main/ns" xmlns:bar="http://secondary/ns">
  <foo:child bar:attr="1234">some data</foo:child>
</foo:root>

or

<bar:root xmlns:bar="http://main/ns" xmlns:foo="http://secondary/ns">
  <bar:child foo:attr="1234">some data</bar:child>
</bar:root>

or

<root xmlns="http://main/ns" xmlns:baz="http://secondary/ns">
  <child baz:attr="1234">some data</child>
</root>

So if you extract namespaces dynamically from them, your subsequent code will inevitably (and unnecessarily) break.

Use actual namespace URIs in your code and choose prefixes to your liking - prefixes are ephemeral, they don't have to match up with the XML files. XML is strongly typed data, treat it accordingly.

In other words, namespace prefixes are aliases, a convenience facility, they only exist in serialized data. They exist in XML, they don't exist in the DOM. They exist in your application's XPath expressions, they don't exist in the abstract tree the XPath expressions are parsed into. Those are two completely separate domains. If the same prefixes are used in both domains, that's entirely coincidental. Don't build application logic that transfers prefixes from one domain to the other, as this is bound to break.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • The reason I need this is because I'm working with a legacy set of xmls which completely abuse the xml standard. They use the xml namespace to prefix mapping as an actual way to store data (that is, the mappings are not used in the xml at all, but the xml itself was meant to describe an ontology that maps prefixes to namespaces). The right thing would have been to have something like a element in the legacy xml, but since this is not the case... – eladidan Jan 05 '15 at 11:08
  • Can you post an example of these XML files so I can see them myself? – Tomalak Jan 05 '15 at 11:26
  • Not really, might be too sensitive. But for all intents and purposes you can imagine that in my example, prefixes bar and baz are never used again in the xml, except for in strings. Something like: – eladidan Jan 05 '15 at 12:26
  • 1
    That's indeed some way of misusing XML namespaces. I'm afraid I can't say a lot to that, the purpose of this answer was it to save you from shooting yourself in the foot by misusing XML namespaces yourself. – Tomalak Jan 05 '15 at 12:49
  • 1
    And indeed it's a good answer in that regard. But the question was asked because someone already took a big ol' gun and shot both my feet – eladidan Jan 05 '15 at 15:11