根据具体情况,如何根据需要制作XSD元素?

时间:2012-10-23 19:29:41

标签: xsd xsd-validation

我们有Person元素的定义,我们希望根据需要使用不同的元素 他们在做什么。例如,如果他们要添加Person,则需要不同的元素 要发送而不是更新人员。在示例的下方,Person类型当前是重复的,其中 当然是错的。有没有一种很好的方法在xsd中表示这个,所以我们可以重用Person类型。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="Person">
        <xs:annotation>
            <xs:documentation>This is the definition when changing a person</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="PartyName" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="0" maxOccurs="1"/>
            <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Person">
        <xs:annotation>
            <xs:documentation>This is the definition when adding a person</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="PartyName" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="1" maxOccurs="1"/>
            <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>   
</xs:schema>

1 个答案:

答案 0 :(得分:4)

为Person元素提供两种不同类型的最简单方法是在您考虑的两个不同上下文中使用Person的局部声明。例如,您可能会说:

<xs:element name="Add">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Person" type="AddPerson"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="Update">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Person" type="ChangePerson"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

此示例假定您已将两个复杂类型重新定义为AddPerson和ChangePerson。

如果另外要使两个复杂类型明确相关,则可以通过限制从通用Person类型派生它们。

<xs:complexType name="Person">
  <xs:annotation>
    <xs:documentation>This is the generic 
      definition for persons</xs:documentation>
  </xs:annotation>
  <xs:sequence>
    <xs:element name="PartyName" type="xs:string" 
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="GenderCode" type="GenderCode_Type" 
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="BirthDate" type="xs:date" 
                minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ChangePerson">
  <xs:annotation>
    <xs:documentation>This is the definition 
      when changing a person</xs:documentation>
  </xs:annotation>
  <xs:complexContent>
    <xs:restriction base="Person">
      <xs:sequence>
        <xs:element name="PartyName" type="xs:string" 
                    minOccurs="0" maxOccurs="1"/>
        <xs:element name="GenderCode" type="GenderCode_Type" 
                    minOccurs="0" maxOccurs="1"/>
        <xs:element name="BirthDate" type="xs:date" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:sequence>        
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="AddPerson">
  <xs:annotation>
    <xs:documentation>This is the definition 
      when adding a person</xs:documentation>
  </xs:annotation>
  <xs:complexContent>
    <xs:restriction base="Person">
      <xs:sequence>
        <xs:element name="PartyName" type="xs:string" 
                    minOccurs="1" maxOccurs="1"/>
        <xs:element name="GenderCode" type="GenderCode_Type" 
                    minOccurs="1" maxOccurs="1"/>
        <xs:element name="BirthDate" type="xs:date" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:sequence>        
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>  

这里,泛型类型Person与AddPerson类型相同;我已经使用空白限制定义了AddPerson,只是为了从泛型类型派生两个特定于操作的类型的对称性。

在您的类型之间实现这种明确的关系实际上是否有助于您实现目标,当然,部分取决于您的系统对您的模式类型定义的使用。

相关问题