XSD允许扩展,兼容性和验证

时间:2012-09-25 17:43:00

标签: jaxb xsd

我想为一个应用程序创建一个XSD,并为另一个应用程序创建另一个XSD(仅通过添加元素)。

我希望第二个应用程序生成的XML文件对第一个应用程序有效。

我试过了:

第一个XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns="example"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="example"
       elementFormDefault="qualified" attributeFormDefault="qualified">

  <xs:complexType name="typeA">
    <xs:sequence>
      <xs:element name="elA" type="xs:string" />
      <xs:any namespace="##any" minOccurs="0" processContents="lax" />
    </xs:sequence>
  </xs:complexType>

  <xs:element name="root" type="typeA" />
</xs:schema>

第二次XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns="example"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="example">
  <xs:redefine schemaLocation="firstXSD.xsd">
    <xs:complexType name="typeA">
      <xs:complexContent>
       <xs:extension base="typeA">
         <xs:sequence>
           <xs:element name="newElement" type="xs:string" />
         </xs:sequence>
       </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

必须对第一个XSD(但不是第二个)有效的XML示例:

<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="example">
  <elA>MyString</elA>
</root>

必须对两者 XSD

有效的XML示例
 <?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="example">
  <elA>MyString</elA>
  <newElement>MyNewString</newElement>
</root>

之前的XSD违反了“独特的粒子归因”,我想要修复它。 我可以编辑两个XSD,但我希望能够在完成第二个之前分发第一个。

如何实现这一点(在JAXB检查时两个模式都必须有效)?

由于

2 个答案:

答案 0 :(得分:1)

你真正想要的是限制。当您进行扩展时,通配符仍然是内容模型的一部分,这就是您有唯一粒子归因违规的原因。你实际上要做的是将通配符替换为更具限制性的东西,即特定元素。

答案 1 :(得分:1)

有些人可能会说:如果您可以编辑这两个XSD,为什么还要重新定义?

我将向您展示如何使其与XSD重新定义一起使用,至少从XSD的角度来看。但是,考虑到JAXB的限制,它不能用于开箱即用。如果您还使用自动XSD重构作为额外步骤,那么您可以使其工作,并且在此过程中,您将保留使用xsd:redefine时看到的价值主张。

所以,在此之前,这是另一种使用合成的方法,但没有xsd:redefine;从维护和验证的角度来看,您获得了相同的价值和用途。

我会将您的第一个XSD称为Model1,将您的第二个XSD称为Model2。我将从一个XSD开始,它将为您提供xs:redefine所具有的“重复使用组合”方面。

常见项目, xsd-allow-extension-compatibility-and-validation-common-items.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns="example" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       targetNamespace="example" 
       elementFormDefault="qualified" attributeFormDefault="qualified"> 

  <xs:group name="typeA"> 
    <xs:sequence> 
      <xs:element name="elA" type="xs:string" /> 
    </xs:sequence> 
  </xs:group> 

  <xs:element name="root" type="typeA" /> 
</xs:schema> 

Model1“items”, xsd-allow-extension-compatibility-and-validation-model1-items.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns="example" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       targetNamespace="example" 
       elementFormDefault="qualified" attributeFormDefault="qualified"> 

  <xs:complexType name="typeA"> 
    <xs:sequence> 
      <xs:group ref="typeA" /> 
      <xs:any namespace="##any" minOccurs="0" processContents="lax" /> 
    </xs:sequence> 
  </xs:complexType>  
</xs:schema> 

Model2“items”, xsd-allow-extension-compatibility-and-validation-model2-items.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns="example" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       targetNamespace="example" 
       elementFormDefault="qualified" attributeFormDefault="qualified"> 

  <xs:complexType name="typeA"> 
    <xs:sequence> 
      <xs:group ref="typeA" /> 
        <xs:element name="newElement" type="xs:string" />
    </xs:sequence> 
  </xs:complexType>  
</xs:schema> 

如果将Common Items和Model1,或Common Items和Model2传递给JAXB编译器,它将按照您希望的方式创建类。为了便于使用(测试)和插图,我又创建了两个XSD:

型号1:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns="example" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       targetNamespace="example" 
       elementFormDefault="qualified" attributeFormDefault="qualified"> 
    <xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-common-items.xsd"/>
    <xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-model1-items.xsd"/>
</xs:schema> 

模型2:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns="example" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-common-items.xsd"/>
    <xs:include schemaLocation="xsd-allow-extension-compatibility-and-validation-model2-items.xsd"/>
</xs:schema>

这是你在Model1上再次运行xjc时得到的:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "typeA", propOrder = {
    "elA",
    "any"
})
public class TypeA {

    @XmlElement(required = true)
    protected String elA;
    @XmlAnyElement(lax = true)
    protected Object any;

    /**
     * Gets the value of the elA property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getElA() {
        return elA;
    }

    /**
     * Sets the value of the elA property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setElA(String value) {
        this.elA = value;
    }

    /**
     * Gets the value of the any property.
     * 
     * @return
     *     possible object is
     *     {@link Element }
     *     {@link Object }
     *     
     */
    public Object getAny() {
        return any;
    }

    /**
     * Sets the value of the any property.
     * 
     * @param value
     *     allowed object is
     *     {@link Element }
     *     {@link Object }
     *     
     */
    public void setAny(Object value) {
        this.any = value;
    }

}

...当你再次运行xjc Model2时:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "typeA", propOrder = {
    "elA",
    "newElement"
})
public class TypeA {

    @XmlElement(required = true)
    protected String elA;
    @XmlElement(required = true)
    protected String newElement;

    /**
    * Gets the value of the elA property.
    * 
    * @return
    *     possible object is
    *     {@link String }
    *     
    */
    public String getElA() {
        return elA;
    }

    /**
    * Sets the value of the elA property.
    * 
    * @param value
    *     allowed object is
    *     {@link String }
    *     
    */
    public void setElA(String value) {
        this.elA = value;
    }

    /**
    * Gets the value of the newElement property.
    * 
    * @return
    *     possible object is
    *     {@link String }
    *     
    */
    public String getNewElement() {
        return newElement;
    }

    /**
    * Sets the value of the newElement property.
    * 
    * @param value
    *     allowed object is
    *     {@link String }
    *     
    */
    public void setNewElement(String value) {
        this.newElement = value;
    }

}

Model1和Model2 XSD将完全按照您的预期方式验证您的XML。

下面是一个显示XSD文件之间关系的图表。绿色表示“xsd:include”,箭头指向“包含”。

QTAssistant XSD file diagram

更新:根据@Kevin的评论,我注意到你在重新定义的新元素上没有maxOccurs。在这种情况下,您可以使用一次重新定义,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="example" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="xsd-allow-extension-compatibility-and-validation.xsd">
    <xsd:complexType name="typeA">
      <xsd:complexContent>
        <xsd:restriction base="typeA">
          <xsd:sequence>
            <xsd:element name="elA" type="xsd:string" /> 
            <xsd:element name="newElement" type="xsd:string" />
        </xsd:sequence>
        </xsd:restriction>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:redefine>
</xsd:schema>

唯一的问题似乎是JAXB(最新版)仍然使用通配符生成一个类。

更新2:根据Kevin的评论,两个避免两个重新定义,应该使用一个组而不是xsd:any。

如果您实际上计划使用多个元素来扩展新模型,请继续阅读。以下是唯一的方法,需要使用一组来进一步细化任何粒子。

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="example" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="example" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="xsd-allow-extension-compatibility-and-validation.xsd">
    <xsd:complexType name="typeA">
      <xsd:complexContent>
        <xsd:restriction base="typeA">
          <xsd:sequence>
                  <xsd:element name="elA" type="xsd:string" /> 
                  <xsd:group ref="group1" minOccurs="0">
        </xsd:sequence>
        </xsd:restriction>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:redefine>
        <xsd:group name="group1">
          <xsd:sequence>
            <xsd:element name="newElement" type="xsd:string" />
        </xsd:sequence>

        </xsd:group>
</xsd:schema>

最终结果是新的XSD可用于验证Model1,而原始文件仍为Model1。

相关问题