带有不同类型和重新排序类型列表的 XML 的 XSD [英] XSD for XML's with list of different and reorder types

查看:29
本文介绍了带有不同类型和重新排序类型列表的 XML 的 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,如何创建成功验证以下 xml 的 xsd 架构:
有效的 XML 1

Tell me please, how can I create xsd schema, which successfully validate the following xml:
Valid XML 1

    <?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
  <list>
    <typeA>
      <value>test string value 123</value>
    </typeA>
    <typeB>
      <value>test string value 456</value>
    </typeB>
    <typeC>
      <value>test string value 789</value>
    </typeC>
  </list>
</start>

有效的 XML 2

    <?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
  <list>
    <typeB>
      <value>test string value 456</value>
    </typeB>
    <typeC>
      <value>test string value 789</value>
    </typeC>
  </list>
</start>

有效的 XML 3

    <?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
  <list>
    <typeC>
      <value>test string value 789</value>
    </typeC>
    <typeC>
      <value>test string value 123</value>
    </typeC>
  </list>
</start>

有效的 XML 4

    <?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
  <list>
    <typeC>
      <value>test string value 789</value>
    </typeC>
  </list>
</start>

具有重新排序类型的无效 XML 5

<?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
    <list>
        <typeB>
            <value>test string value 456</value>
        </typeB>
        <typeC>
            <value>test string value 789</value>
        </typeC>
        <typeA>
            <value>test string value 123</value>
        </typeA>
    </list>
</start>

<小时>

我编写了 XSD,但它不适用于具有重新排序类型 (XML 5) 的 XML:


I wrote XSD, but it doesn't work for XML with reorder types (XML 5):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="typeC">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="value" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="typeB">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="value" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="typeA">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="value" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="start">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="list"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="typeA" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="typeB" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="typeC" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

有没有其他方法可以实现这一目标?
该解决方案应该适用于上面列出的 5 个不同的 XML 文件.

Is there any other way to achieve this?
The solution should work for 5 different XML files, listed above.

提前致谢!

推荐答案

试试这个,它有效:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="start">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="list" type="list"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="list">
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="typeA" type="generic" />
        <xs:element name="typeB" type="generic" />
        <xs:element name="typeC" type="generic" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="generic">
    <xs:sequence>
      <xs:element name="value" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

这篇关于带有不同类型和重新排序类型列表的 XML 的 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆