xml 在 XSD 中表示对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4740221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:30:31  来源:igfitidea点击:

Represent a List of Objects in XSD

xmlxsd

提问by édipo Féderle

How I can represent a list of objects in XSD, for example, given a XML like this?

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

 <msgBody>
  <Contato>
   <cdEndereco>11</cdAreaRegistro>
   <cdBairro>99797781</nrLinha>
   <email>[email protected]</email>
  </Contato>
  <Contato>
   <cdEndereco>11</cdAreaRegistro>
   <cdBairro>99797781</nrLinha>
   <email>[email protected]</email>
  </Contato>
 </msgBody>

How I can merge it into a list of object type Contato?

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

回答by Lachezar Balev

I may suggest the following schema (even though your XML is broken as pasted):

我可能会建议以下架构(即使您的 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>

回答by dogbane

Use a sequence as shown below:

使用如下所示的序列:

<?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>