开发者

XML Schema order indicators confusion

I've been studying up how to write an XML Schema and I'm stumped on XSD ordering indicators like xs:sequence, xs:all, xs:choice. There seem to be only 3 of them and they're required in complex types. But what if I have the XML like the following:

<row>
    <name>John</name>
    <city>LA</city>
    <country>France</country>
</row>

In which the 3 elements inside <row/> can appear in any order i.e. can appear before <city> and <name> etc. like this:

<ro开发者_运维问答w>
    <country>France</country>
    <city>LA</city>
    <name>John</name>
</row>

Does this mean I'll have specify a new <xs:sequence> for each sequence of elements. What if I have something like 20 elements with no specific order. Is there some shortcut here? Am I missing something?

UPDATE: I can't use <xs:all> because I might leave some elements. All of them are not required.


<xsl:all> is still your correct answer here I believe. Each element inside an <xs:all> compositor can have a minOccurs attribute of zero. The result is a set of elements which may occur in any order and any element may occur exactly zero or one times. For example, the below requires that row contains one name element, zero or one city elements and zero or one country elements in any order.

<xs:element name="row"> 
    <xs:complexType>
        <xs:all> 
            <xs:element ref="name"/> 
            <xs:element ref="city" minOccurs='0'/> 
            <xs:element ref="country" minOccurs="0"/>
        </xs:all>
    </xs:complexType>

I may have misunderstood your requirement though.


Found the answer to this question. Instead of using <xs:all> use <xs:choice> with minOccurs="0" and maxOccurs="unbounded". Now the elements are not only optional they can also appear in any order.

<xs:element name="row">
 <xs:complexType>
   <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="name" /> 
     <xs:element name="city" /> 
     <xs:element name="country" />
   </xs:choice>
 </xs:complexType>
</xs:element>


<xs:all> permits items in any order. Just use it instead of <xs:sequence>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜