xml XSD all 和 XSD 序列之间的中间方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/839079/
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 12:29:17  来源:igfitidea点击:

Middle way between XSD all and XSD sequence

xmlxsd

提问by Bart van Heukelom

I'm defining a user element with XSD. For this example, a user has a name, email and one or more nationalities. I've tried:

我正在用 XSD 定义一个用户元素。对于此示例,用户具有姓名、电子邮件和一个或多个国籍。我试过了:

<xs:all>
  <xs:element name="name" blabla />
  <xs:element name="email" blabla />
  <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" />
</xs:all>

However, that is illegal. Apparently elements inside an "All" can only occur one time (or not at all). I could fix this by changing the All to a Sequence, but then people would have to enter the properties in the exact order, which I actually don't care about.

然而,这是非法的。显然,“全部”中的元素只能出现一次(或根本不出现)。我可以通过将 All 更改为 Sequence 来解决这个问题,但是人们必须按照确切的顺序输入属性,而我实际上并不关心这些。

Is there a combination of these two available? Not according to http://www.w3schools.com/Schema/schema_complex_indicators.asp, but maybe it's hidden (or my inexperienced eyes don't see it).

有没有这两者的组合?不是根据http://www.w3schools.com/Schema/schema_complex_indicators.asp,但也许它是隐藏的(或者我没有经验的眼睛看不到它)。

By intuition, I also tried:

凭直觉,我也试过:

<xs:all>
  <xs:element name="name" blabla />
  <xs:element name="email" blabla />
  <xs:sequence>
    <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
</xs:all>

But that's unfortunately invalid.

但不幸的是,这是无效的。



Here is the current, real, piece of XSD:

这是当前真实的 XSD 片段:

  <!-- user -->
  <xs:complexType name="user">
    <xs:sequence>
      <xs:element name="firstname" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="appendix" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="lastname" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="zipcode" type="xs:string" minOccurs="1" maxOccurs="1" />
      <xs:element name="city" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="email" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="country" type="country" minOccurs="1" maxOccurs="1"/>
      <xs:element name="nationality" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

采纳答案by jelovirt

Your code should be valid in XSD 1.1. For XSD 1.0 you have to find a workaround.

您的代码应该在 XSD 1.1 中有效。对于 XSD 1.0,您必须找到一种解决方法。

回答by marc_s

Could you just turn your "nationality" thingie into its own complexType and then use that new complex type inside your xs:all?

你能不能把你的“国籍”东西变成它自己的 complexType,然后在你的 xs:all 中使用那个新的复杂类型?

<xs:complexType name="NationalityType">
  <xs:sequence>   
    <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

<xs:all>
  <xs:element name="name" blabla />
  <xs:element name="email" blabla />
  <xs:element name="nationalities" type="NationalityType" />
</xs:all>

I don't have anything at hand to test this, so this is really just off the top of my head..... give it a try!

我手头没有任何东西可以测试这个,所以这真的只是我的头顶......试一试!

EDIT: tested it by now - it works, the only minor price to pay is that your XML will have to look something like this:

编辑:现在已经测试过了 - 它可以工作,唯一要付出的代价是你的 XML 必须看起来像这样:

<....>
  <email>......</email>
  <nationalities>
    <nationality>ABC</nationality>
    <nationality>CDE</nationality>
  </nationalities>
  <name>.....</name>
</.....>

So you get an extra node that will contain the arbitrary long list of <nationality>items.

所以你会得到一个额外的节点,它将包含任意长的<nationality>项目列表。

Marc

马克

回答by fangster

Just come across the a similar problem (I wanted to have any number of each element in any order) and solved it with a sequence of choices. Using the example above:

刚遇到一个类似的问题(我想按任何顺序排列任意数量的每个元素)并通过一系列选择解决它。使用上面的例子:

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name='user'>
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
          <xs:element name="name" type="xs:string" />
          <xs:element name="email" type="xs:string" />
          <xs:element name="nationality" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This allows you to have any number of name, email and nationality in any order.

这允许您以任何顺序拥有任意数量的姓名、电子邮件和国籍。

回答by Jeremy Stein

I think what you're looking for would go against the intent of XML. It would seems strange to have a valid XML fragment like this:

我认为您正在寻找的内容与 XML 的意图背道而驰。有这样一个有效的 XML 片段似乎很奇怪:

<user>
  <nationality/>
  <name/>
  <nationality/>
  <email/>
  <nationality/>
</user>

It sounds like you're asking for something like what marc_s proposed:

听起来您要求的内容类似于 marc_s 提出的内容:

<user>
  <name/>
  <email/>
  <nationality/>
  <nationality/>
  <nationality/>
<user>

which needs to get pushed into:

需要推入:

<user>
  <name/>
  <email/>
  <nationalities>
     <nationality/>
     <nationality/>
     <nationality/>
  </nationalities>
<user>

回答by ugo

For XSD 1.0 the suggestion from leppie works.

对于 XSD 1.0,leppie 的建议有效。

The XSD

XSD

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name='user'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="email" type="xs:string" />
        <xs:choice minOccurs='0' maxOccurs='unbounded'>
          <xs:element name="nationality" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

A sample XML document that validates against the schema

根据模式进行验证的示例 XML 文档

<user>
  <name>Name</name>
  <email>[email protected]</email>
  <nationality>Italian</nationality>
  <nationality>Japanese</nationality>
  <nationality>Alien</nationality>
</user>

And validation e.g. using xmllint

和验证,例如使用xmllint

xmllint --noout --schema test.xsd test.xml
test.xml validate

回答by AllenG

Or, since "USER" will be set up with multiple child elements, why not set it up as a complex type? Something like this should work.

或者,既然“USER”将设置多个子元素,为什么不将其设置为复杂类型?像这样的事情应该有效。

<xs:complexType>
  <xs:sequence>
    <xs:element name="Name" type="xs:string" />
    <xs:element name="Password" type="xs:string" />
    <xs:element minOccurs="1" maxOccurs="unbounded" name="Nationality" type="xs:string" />
  </xs:sequence>
</xs:complexType>

回答by leppie

xs:choicewont work? If not, just wrap that in a sequence or vice versa.

xs:choice行不通?如果没有,只需将其按顺序包装,反之亦然。