简体   繁体   中英

XML Schema Display Elements in Unordered pattern

I have a Schema XSD File that has the elements listed in the Sequence . The problem is that Sequence forces the XML file to list the elements in the order . Now, I want to use the choice tag. But according to the xml specification, choice allows only one of the elements contained in the declaration to be present within the containing element. Also, I can't use All tag because I want the occurrence more than once . But, I want to display all the A, B, C, D and E in the un-ordered pattern . Any suggestions?

Xml Schema File

<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="A" />
        <xsd:element name="B" />
        <xsd:element name="C" />
        <xsd:element name="D" />
        <xsd:element name="E" />
    </xsd:sequence>
</xsd:complexType>

XML File (I want these elements to be in any order)

<a>a</a>
<b>b</b>
<c>c</c>
<d>d</d>
<e>e</e>

This XSD,

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="r">
    <xsd:complexType>
      <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="A" />
        <xsd:element name="B" />
        <xsd:element name="C" />
        <xsd:element name="D" />
        <xsd:element name="E" />
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

will validate elements A through E as children of r in any order and in any number of occurrences.

Explanation

The occurrence constraints on the xsd:choice element allow the choice itself to be repeated the indicated number of times. So, you can make a choice between the child elements zero times and get an empty content model for r . For one time, you can choose any of the children. For a second time, you can choose the same child you did the first time, or any other child. As you can see, this continued operation allows any number of the children elements to appear in any order.

Examples

All of the following XML documents would be valid against the above XSD:

  • <r></r>
  • <r><A/><A/></r>
  • <r><A/><A/><A/></r>
  • <r><A/><B/><A/><C/><A/><D/><A/><E/><A/></r>
  • <r><E/><D/><C/><B/><A/></r>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM