Embedded DOM Elements may lose locally defined namespace prefixes #452
Description
JAXB's marshaller allows embedding generic DOM Elements in the following
locations, as far as I am aware of.
- A value of a JAXBElement<>.
- An Object-typed field/property annotated with @xmlelement.
Such embedded elements may have attributes of which values are qualified names.
The most significant those attributes might be xsi:type attribute. Although the
prefixes used in those qualified names are defined like normal XML namespaces
using xmlns: syntax, XML processors cannot change them because they do
not know which attribute has a qualified name as its value.
However, while writing the embedded element, JAXB RI's marshaller omits a local
namespace prefix definition if it is already defined outside the element where
the embedded element's contents are going to be written into. This causes a
problem when local attributes of which values are qualified names using the
prefix. Since the prefix is no longer defined in the resulted XML document,
readers of the document cannot determine the namespace URI associated with the
qualified name.
I believe that the marshaller should always emit a local prefix definition even
if its namespace URI is already assigned to another prefix. In fact, the
marshaller currently emits a prefix definition even if it is defined as the
default namespace. Please look into the attached test case and find
testSuccess2() do this.
P.S. Although the attached case might seem a bit synthetic, I actually stumbled
upon the issue while writing a piece of code for a real world project.
Environment
Operating System: All
Platform: All
Affected Versions
[2.0.5]