简体   繁体   中英

xml element validation against xsd - how to define unordered element?

Now I'm validating an XML like this:

<?xml version="1.0" encoding="utf-8"?>
  <all>
     <allElem>
      <a>12345</a>        
      <b>2</b>              
      <c>3</c>                  
      <d>                               
        <d1>4</d1>              
        <d2>5</d2>               
        <d3>6</d3>           
      </d>
      <d>                               
       <d1>7</d1>               
       <d2>8</d2>               
       <d3>9</d3>
      </d>                      
   </allElem>
</all>

I wrote a simple XSD like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="all">
  <xs:complexType>
    <xs:sequence>
    <xs:element name="allElem" maxOccurs="1">
        <xs:complexType>
            <xs:sequence>
            <xs:element name="a" type="xs:string"/>
            <xs:element name="b" type="xs:string"/>
            <xs:element name="c" type="xs:string"/>
            <xs:element name="d" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>    
                    <xs:sequence>
                    <xs:element name="d1" type="xs:string"/>
                    <xs:element name="d2" type="xs:string"/>
                    <xs:element name="d3" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:sequence>
  </xs:complexType> 
   </xs:element>    

 </xs:schema>

My question is: I'm using xs:sequence , but I don't want my xml element to be ordered, I can't use xs:all, because it only allow element occurs once max, but my xml element "d" should be able to appear more than once. Is there a solution in xsd that I can define an unordered element appears once or appears many times? In other words, I want to know is there such an xsd tag that can define unordered elements which can appear more than once.

In XSD 1.0:

  • you can use xs:all in place of xs:sequence, but then each element can only appear (0 or 1) times.

  • you can use xs:choice with minOccurs=0 maxOccurs=unbounded, but then each element can appear any number of times

In XSD 1.1:

  • you can use xs:all and specify the min/max cardinality for each element (which is what you are asking for)

XSD 1.1 is currently available in Xerces (beta version) and Saxon (current release Saxon-EE 9.5).

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