预期不同的父子命名空间前缀

时间:2013-12-06 11:56:29

标签: java xml jaxb xsd

我有两个xsds,我使用xjc生成了jaxb pojos。我使用jaxb marshaller和jaxb-impl-2.2.6生成了XML 为此,我将NamespacePrefixMapper重写为MyNamespacePrefixMapper

Parent.xsd

 <xs:schema targetNamespace="parent" 
       elementFormDefault="qualified" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       xmlns:Env="Parent" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:child="urn:xsd:child">
<xs:import namespace="urn:xsd:child" schemaLocation="child.xsd"/>
<xs:element name="parent">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="child" type="child:child1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
 </xs:schema>

child.xsd

  <xs:schema xmlns="urn:xsd:child" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:xsd:child">
<xs:element name="child" type="child1"/>
<xs:complexType name="child1">
    <xs:sequence>
        <xs:element name="Id" type="Max20Text"/>    
    </xs:sequence>
</xs:complexType>
<xs:simpleType name="Max20Text">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:maxLength value="20"/>
    </xs:restriction>
</xs:simpleType>

XmlMarshallingTest.java

 import javax.xml.bind.*;
 import javax.xml.stream.XMLStreamException;
 import org.junit.Test;
 import parent.Parent;
 import xsd.child.Child1;
  public class XmlMarshallingTest {


  @Test
   public void testXmlMarshalling() throws  JAXBException, XMLStreamException{
    Parent envelope = new Parent();
    Child1 businessApplicationHeaderV01 = new Child1();
    businessApplicationHeaderV01.setId("ABC123");
    envelope.setChild(businessApplicationHeaderV01);
    JAXBContext context = JAXBContext.newInstance(envelope.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper());
    marshaller.marshal(envelope, System.out);
    }
}

MyNamespacePrefixMapper.java

    import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
    import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

    public class MyNamespacePrefixMapper extends NamespacePrefixMapper {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean arg2) {
    if("Parent".equals(namespaceUri)) {
        return "parentPrefix";
    } else if("urn:xsd:child".equals(namespaceUri)) {
        return "childPrefix";
    }
    return "defaultPrefix";
}
  }

生成的xml就像

 <parentPrefix:parent xmlns:childPrefix="urn:xsd:child" xmlns:parentPrefix="parent">
   <parentPrefix:child>
    <childPrefix:Id>ABC123</childPrefix:Id>
   </parentPrefix:child>
 </parentPrefix:parent>

在这里,我的问题是,我希望xml看起来像下面的

 <parentPrefix:parent xmlns:childPrefix="urn:xsd:child" xmlns:parentPrefix="parent">
  <childPrefix:child>
    <childPrefix:Id>ABC123</childPrefix:Id>
  </childPrefix:child>
 </parentPrefix:parent>

我希望子标记的前缀为“ childPrefix ”,但它会显示“ parentPrefix ” 使用前缀“ parentPrefix

生成父标记

环境描述

Maven 3.0.4

Java版本:1.7.0_04

操作系统:Windows 7

1 个答案:

答案 0 :(得分:2)

您的模式将parent元素定义为在父模式自己的targetNamespace中具有名为child的子元素,其类型恰好来自子命名空间。如果您希望父级使用子模式中定义的child元素(因此在urn:xsd:child命名空间中),则代替

<xs:element name="child" type="child:child1"/>

你需要

<xs:element ref="child:child"/>