XSD 用于定义元素的复杂类型 OR 数组

XSD to define complex type OR array of elements

提问人:Vlad 提问时间:12/20/2022 更新时间:12/20/2022 访问量:253

问:

如何为 XSD 文件中的元素定义 OR 逻辑? 不幸的是,我无法对“OLD STYLE BLOCK”结构进行更改,我只是在扩展现有的XML结构。

我唯一的想法是像下面的这个示例XSD,但我有解析器错误 我希望我能将 xsd:choice 和 xsd:sequence 包含在 xs:complexType 中

我可以以某种方式使用 Version=“2.0” 向 xsd 添加额外的逻辑吗?

<?xml version="1.0"?>
<MyStruct Version="2.0">

   <!-- xs should allow "NEW STYLE BLOCK" OR "OLD STYLE BLOCK" with array of id -->
   <!-- one of them should present !!!! -->

  <!-- NEW STYLE BLOCK my complex type-->
  <IDArray> <
    <IDSet>
        <ID>89</ID>
        <ID>1</ID>
        <ID>2</ID>
    </IDSet>
    <IDSet>
        <ID>3</ID>
        <ID>4</ID>
    </IDSet>
  </IDArray>
  <!-- end of NEW STYLE BLOCK -->

  <!-- OLD STYLE BLOCK  array of elements  -->
  <ID>5</ID>
  <ID>7</ID>
  <!-- end of OLD STYLE BLOCK -->

  <Type>Some type</Type>  <!-- mandatory element -->
  <ReadableName>Human readable name</ReadableName>  <!-- mandatory element -->
</MyStruct>

建议的 XSD

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

  <xs:element name="MyStruct">
    <xs:complexType>

        <xs:choice>
            <xs:element name="IDArray" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                      <xs:choice maxOccurs="unbounded">
                        <xs:element name="IDSet" minOccurs="1" maxOccurs="unbounded">
                            <xs:complexType>
                                  <xs:choice maxOccurs="unbounded">
                                      <xs:element name="ID" minOccurs="1" maxOccurs="unbounded" />
                                  </xs:choice>
                            </xs:complexType>
                        </xs:element>
                      </xs:choice>
                </xs:complexType>
            </xs:element>

            <xs:element name="ID" minOccurs="0" maxOccurs="unbounded" />
        </xs:choice>

        <xs:sequence>
            <xs:element name="Type" type="xs:string" />
            <xs:element name="ReadableName" type="xs:string" />
        </xs:sequence>  <!--  error in this line  -->

      <xs:attribute name="Version" type="xs:string" />

    </xs:complexType>
  </xs:element>
</xs:schema>

错误消息,我假设 xsd:choice 和 xs:sequence 不允许同时使用

./MyStruct.xsd:: element sequence: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))).
xml xsd 解析 xsd 验证 xml 验证

评论


答:

1赞 Martin Honnen 12/20/2022 #1

尝试

  <xs:element name="MyStruct">
    <xs:complexType>
      <xs:sequence>

        <xs:choice>
            <xs:element name="IDArray" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                      <xs:choice maxOccurs="unbounded">
                        <xs:element name="IDSet" minOccurs="1" maxOccurs="unbounded">
                            <xs:complexType>
                                  <xs:choice maxOccurs="unbounded">
                                      <xs:element name="ID" minOccurs="1" maxOccurs="unbounded" />
                                  </xs:choice>
                            </xs:complexType>
                        </xs:element>
                      </xs:choice>
                </xs:complexType>
            </xs:element>

            <xs:element name="ID" minOccurs="0" maxOccurs="unbounded" />
        </xs:choice>

        <xs:element name="Type" type="xs:string" />
        <xs:element name="ReadableName" type="xs:string" />

     </xs:sequence> 

     <xs:attribute name="Version" type="xs:string" />


    </xs:complexType>
  </xs:element>