JAXB adds a namespace to the parent, but not to the children contained in

I compiled XSD and used JAXB to create classes from 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>

Documented marker -

<?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 element <spam>uses the default namespace. I would like to use this namespace ns2. The schema (mytypes.xsd) expresses the fact that it is <spam>contained within <bar>, which in the XML instance is associated with the namespace ns2.

I broke my head over this for more than a week, and I would like the prefix to ns2appear in <spam>. What should I do?

It is 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>
+5
source share
6

, @XmlElement, , . , xml

+2

, : ns2.

:

    Bar bar = new Bar();
    bar.setSpam("s");

    MyDoc myDoc = new MyDoc();
    myDoc.setBar(bar);

    JAXBContext context = JAXBContext.newInstance("org.mydoc");

    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(myDoc, System.out);

:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myDoc xmlns="http://www.mydoc.org" xmlns:ns2="http://www.mytypes.com"><ns2:bar><ns2:spam>s</ns2:spam></ns2:bar></myDoc>

JAXB:

xjc version "JAXB 2.1.3 in JDK 1.6"
JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build IBM JAXB 2.1.3 in JDK 1.6)

EDIT:

Bar.java :

@XmlElement(required = true)
protected String spam;

XmlElement . Javadoc: http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElement.html#namespace()

@XmlSchema com.mytypes. @XmlSchema / package-info.java?

0

package-info.java , , elementFormDefault = "", Jaxb.

, , package-info.java .class .

.

0

, Maven 2 jaxws-maven-plugin.

 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jaxws-maven-plugin</artifactId>

- maven-compiler-plugin java 1.5. / 1.6 .

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        </configuration>
 </plugin>

, , , .

0

package-info.java @XmlSchema.

elementFormDefault=XmlNsForm.QUALIFIED

sample package-info.java

@XmlSchema(
    namespace="desiredNamespace",   
    // If qualified namespace will be added to all elements
    elementFormDefault = XmlNsForm.QUALIFIED,
    // If qualifies namespace will be added to all attributes
    attributeFormDefault = XmlNsForm.UNQUALIFIED,
    xmlns = {   
        @XmlNs(prefix = "xsd", namespaceURI = "desiredNamespace"),
        @XmlNs(prefix = "xsi", namespaceURI = "anotherLink"),
    }
)
0

, ElementType " ", "". " S" " s" ur, , , , , , . "" ..

- , xslt, xml, xsd JAXB. , xml- Xsd, .

, .

-1

All Articles