JAXB adding namespace to parent but not to the child elements contained
10,688
check if the fields in a generated classed are missing the @XmlElement annotation and if present are they missing the namespace attribute. These two are must in order to get the namespace prefix against every element in your marshaled xml
Related videos on Youtube
Author by
Nishant
Updated on June 04, 2022Comments
-
Nishant almost 2 years
I put together an XSD and used JAXB to generate classes out of it.
Here are my XSDs-
myDoc.xsd : <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns="http://www.mydoc.org" targetNamespace="http://www.mydoc.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mtp="http://www.mytypes.com" elementFormDefault="qualified"> <xs:import namespace="http://www.mytypes.com" schemaLocation="mytypes.xsd" /> <xs:element name="myDoc"> <xs:complexType> <xs:sequence> <xs:element name="crap" type="xs:string"/> <xs:element ref="mtp:foo"/> <xs:element ref="mtp:bar"/> </xs:sequence> </xs:complexType> </xs:element>
mytypes.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://www.mytypes.com" xmlns="http://www.mytypes.com" xmlns:tns="http://www.mytypes.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="foo" type="tns:Foo"/> <xs:element name="bar" type="tns:Bar"/> <xs:element name="spam" type="tns:Spam"/> <xs:simpleType name="Foo"> <xs:restriction base="xs:string"></xs:restriction> </xs:simpleType> <xs:complexType name="Bar"> <xs:sequence> <xs:element ref="spam"/> </xs:sequence> </xs:complexType> <xs:simpleType name="Spam"> <xs:restriction base="xs:string" /> </xs:simpleType> </xs:schema>
The document marshalled is-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <myDoc xmlns:ns2="http://www.mytypes.com"> <crap>real crap</crap> <ns2:foo>bleh</ns2:foo> <ns2:bar> <spam>blah</spam> </ns2:bar> </myDoc>
Note that the
<spam>
element uses the default namespace. I would like it to use thens2
namespace. The schema (mytypes.xsd) expresses the fact that<spam>
is contained within<bar>
which in the XML instance is bound to thens2
namespace.I've broken my head over this for over a week and I would like
ns2
prefix to appear in<spam>
. What should I do?Required :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <myDoc xmlns:ns2="http://www.mytypes.com"> <crap>real crap</crap> <ns2:foo>bleh</ns2:foo> <ns2:bar> <ns2:spam>blah</ns2:spam><!--NS NS NS--> </ns2:bar> </myDoc>
-
Nishant almost 14 yearsI'm using JaxB-2.1.11 with JDK1.5. I haven't hand-edited the generated classes and the package-info.java file. Bar.java is annotated with- @XmlType(name = "Bar" namespace="mytypes.com" propOrder = {"spam"}) package-info.java contains @XmlSchema(namespace="mydoc.org", elementFormDefault=XmlNsForm.QUALIFIED)
-
Nishant almost 14 yearsInterestingly I can marshall a document and unmarshall it back perfectly fine. AFAIU, the XML instance states the <spam> belongs to the null namespace and its definition should not be found