XSD <xs:all> with multi occurrences unordered

牧云@^-^@ 提交于 2019-12-23 23:34:42

问题


I've the following XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<props>
<id>someId</id>
<file_size>
    <size>123</size>
    <unit>MB</unit>
</file_size>
<file_size>
    <size>123</size>
    <unit>MB</unit>
</file_size>
</props>

And the corresponding XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="props">
    <xs:complexType>
        <xs:sequence>
            <xs:element type="xs:string" name="id" />
            <xs:element name="file_size" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element type="xs:int" name="size" />
                        <xs:element type="xs:string" name="unit" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

My goal is to allow unordered elements and i saw ALL indicator solution

but, how can i maintain the file_size behavior? I found some solutions to put:

<file_size>
    <size>123</size>
    <size>125</size>
    <unit>MB</unit>
    <unit>TB</unit>
</file_size>

but i need to have several file_size (with one size and one unit).

To resume, all of the children of props to be in any order but have any number of file_size elements.

How can i handle this?


回答1:


This is quite an awkward requirement for XML Schema 1.0. It would be easy to do either of

  • exactly one each of id and file_size, in either order (using an all)
<xs:all>
    <xs:element type="xs:string" name="id" />
    <xs:element name="file_size">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:int" name="size" />
                <xs:element type="xs:string" name="unit" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:all>
  • any number of id and any number of file_size elements, in any order (using a repeating choice)
<xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element type="xs:string" name="id" />
    <xs:element name="file_size">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:int" name="size" />
                <xs:element type="xs:string" name="unit" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:choice>

but to satisfy your specific requirement of any number of file_size elements but exactly one id is rather clumsy. The clearest way I can see to do it would be to move the file_size definition up to the top level and then ref in zero-or-more occurrences at both possible places:

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

<xs:element name="file_size">
    <xs:complexType>
        <xs:sequence>
            <xs:element type="xs:int" name="size" />
            <xs:element type="xs:string" name="unit" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="props">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="file_size" minOccurs="0" maxOccurs="unbounded" />
            <xs:element type="xs:string" name="id" />
            <xs:element ref="file_size" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:element>



回答2:


You are looking in the right place. You only need to make one small change to your schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:element name="props">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:string" name="id" />
                <xs:element name="file_size" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType>
                        <xs:all>
                            <xs:element type="xs:int" name="size" />
                            <xs:element type="xs:string" name="unit" />
                        </xs:all>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


来源:https://stackoverflow.com/questions/24759628/xsd-xsall-with-multi-occurrences-unordered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!