表示XSD中的对象列表

时间:2011-01-19 20:14:41

标签: xml xsd

如何在XSD中表示对象列表,例如,给定这样的XML?

 <msgBody>
  <Contato>
   <cdEndereco>11</cdAreaRegistro>
   <cdBairro>99797781</nrLinha>
   <email>foo@foo.com</email>
  </Contato>
  <Contato>
   <cdEndereco>11</cdAreaRegistro>
   <cdBairro>99797781</nrLinha>
   <email>foo@foo.com</email>
  </Contato>
 </msgBody>

如何将其合并到对象类型Contato列表中?

2 个答案:

答案 0 :(得分:12)

我可能会建议以下架构(即使你的XML被破坏为粘贴):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="msgBody">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="Contato"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Contato">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cdEndereco"/>
        <xs:element ref="cdBairro"/>
        <xs:element ref="email"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cdEndereco" type="xs:integer"/>
  <xs:element name="cdBairro" type="xs:integer"/>
  <xs:element name="email" type="xs:string"/>
</xs:schema>

答案 1 :(得分:7)

使用如下所示的序列:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="msgBody">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Contato" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:int" name="cdEndereco"/>
              <xs:element type="xs:int" name="cdBairro"/>
              <xs:element type="xs:string" name="email"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>