开发者

Multiple elements in Xml Schema

I have a schema, where there are 3 elements and these 3 elements still have more sub elements. Lets name 1st element as Header, 2nd one as record, 2rd one as footer.

There is one occurence of header, multiple occurences of record and one occurence of footer again.

so the input to schema looks like

header
record..
record..
...
..
footer

now my problem is . 开发者_运维技巧my schema is defined like this

<xs:element minOccurs="1" maxOccurs="1" name="HEADER"> 
<xs:element minOccurs="1" maxOccurs="unbounded" name="Record"> 
<xs:element minOccurs="1" maxOccurs="1" name="FOOTER">

when a file (with a header 2 records and footer)is parsed through this schema. the parser recognizes recognizes the header and both the records and gives an exception after recognizing the 2nd record and doesn't give the footer. How do we define max and min occurs when there are 2 or more elements and middle element has unbounded as max occurs

EDIT:

thanks for the replies. This is my xsd file.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:NS="sample.xsd" xmlns="sample.xsd" elementFormDefault="qualified" targetNamespace="sample.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PTRO">
    <xs:complexType>
      <xs:sequence>


   <xs:element minOccurs="1" maxOccurs="1" name="HEADER">
   <xs:complexType>
    <xs:sequence>
    <xs:sequence>
          <xs:element minOccurs="1" maxOccurs="1" name="header_sub_element">
                <xs:simpleType>
               </xs:simpleType>
              </xs:element>
   </xs:sequence>
          </xs:sequence>
          </xs:complexType>
        </xs:element>


        <xs:element minOccurs="1" maxOccurs="unbounded" name="Record">
          <xs:complexType>
            <xs:sequence>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="Record_sub_element">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="11" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
             </xs:sequence>
          </xs:sequence>
          </xs:complexType>
        </xs:element>
       <xs:element minOccurs="1" maxOccurs="1" name="FOOTER">
   <xs:complexType>
    <xs:sequence>
    <xs:sequence>
          <xs:element minOccurs="1" maxOccurs="1" name="footer_sub_element">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
          <xs:maxLength value="9" />
                   </xs:restriction>
                </xs:simpleType>
              </xs:element>
   </xs:sequence>
          </xs:sequence>
          </xs:complexType>
        </xs:element>
</xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

@ marc_s the problem is when an input file (.txt file) which contains a header, 2 records, footer is parsed using this schema, the parser recognizes header and records...and after this it gives an exception since it doesn't recognize the footer. The max occurs of Record is unbounded. Does this have something to do with the exception ??


You have to use < sequence >< /sequence > instead of the < all >< /all > or < choice >< /choice >.

That should solve the problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜