XSD 使用不按特定顺序排列的 xs:group 元素



我有以下xml结构

<library>
  <propertySet>
    <SUPorganisationId></SUPorganisationId>
    <SUPdataCategory></SUPdataCategory>
    <SUPguId></SUPguId>
    <LIBuserNotice></LIBuserNotice>
  </propertySet>
</library>

属性集中的属性可以出现一次(minHappen="0" maxHappen="1"),可以是任何顺序。当我创建XSD时,我想对一些属性(带有SUP的前缀)进行分组以供进一步使用。所以我提出了以下 xsd 片段。

<xs:element name="propertySet">
  <xs:complexType>
    <xs:all>
      <xs:group ref="CORproperties"/>
      <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:all>
  </xs:complexType>
<xs:element name="propertySet">
<xs:group name="CORproperties">
  <xs:all>
    <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
    <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
    <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
  </xs:all>
</xs:group>

有了这个 xsd,我收到错误,说 xs:all 的使用不正确。我被迫使用 xs:all,因为没有出现的属性顺序。但是如果我使用 xs:sequence,它可以正常工作。谁能指引我走正确的道路?

您可以使用

<xs:extension>来执行此操作。如果以这种方式重构架构,它将正常工作:

警告:它仅在 XSD 1.1 中可用。在 XSD 1.0 中,这是不允许的。

<xs:element name="propertySet">
    <xs:complexType>
     <xs:complexContent>
         <xs:extension base="CORProperties">
             <xs:all>
                 <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
             </xs:all>
         </xs:extension>
     </xs:complexContent>
    </xs:complexType>
</xs:element>
<xs:complexType name="CORProperties">
    <xs:all>
        <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:all>
</xs:complexType>

xs:all 的注解中,我们看到它不能有组:

<all
   id = ID
   maxOccurs = 1 : 1
   minOccurs = (0 | 1) : 1
   {any attributes with non-schema namespace . . .}>
   Content: (annotation?, element*)
</all>

解决方法一:将组更改为复杂类型

当然,这会改变 xml 的结构,但这种方式比选项 2 更具可读性。

解决方法二:接受重复项

<xs:element name="propertySet">
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <xs:group ref="CORproperties"/>
            <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
        </xs:choice>
    </xs:complexType>
</xs:element>
<xs:group name="CORproperties">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:choice>
</xs:group>

更多:

  • XSD 中的所有/序列/选择

最新更新