XSD - 任何顺序和计数的元素(由XERCES使用XSD验证的XML)

时间:2011-02-23 08:04:51

标签: xml xsd xerces

我有XML架构的问题。我需要在三个类型的一个元素内部,但没有任何其他限制,后面恰好出现一个元素output

<command path="app.exe" workingDir="/usr/local/bin">
    <param name="--name" assign="=">anyName</param>
    <switch name="--verbose"/>
    <param name="--config">/etc/app/conf.txt</param>
    <param name="--overriding">~/app/conf.txt</param>
    <switch name="-d"/>
    <param name="--report" assign="=">~/app/report.txt</param>
    <param name="--template">~/app/templates/default.tt</param>
    <string>../t/${testName}/log.txt</string>
    <output>
        <out path="stdout.txt"/>
        <err path="stderr.txt"/>
    </output>
</command>

我只能使用sequenceallchoice,但其中没有一个符合我的要求。顺序 - 按照确切的顺序进行任意次数。全部 - 任何顺序为零或一次。选择 - 只有其中一个。我找到了one solution on this web,但它不适用于Xerces。我试试这个:

<xs:complexType name="commandType">
    <xs:sequence>
        <xs:group ref="gupa"/>
        <xs:element name="output" type="outputType" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="path" use="required" type="value"/>
    <xs:attribute name="workingDir" use="required" type="value"/>
</xs:complexType>

<xs:group name="gupa">
    <xs:choice>
        <xs:element name="env" type="pair" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="param" type="paramType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="switch" type="switchType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="string" type="value" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
</xs:group>

但我收到错误:从元素'switch'开始发现无效内容。预计会有一个'{param,output}'。有一个技巧。

  

如果移动了maxOccurs="unbounded"   从元素到元素choice   架构,然后可以任何元素   以任意数量的任何顺序出现   次。

然而,当我这样做时,我收到错误:属性'maxOccurs'不能出现在元素'choice'中

我在互联网上巡航,但我仍然没有找到我正在寻找的东西。

1 个答案:

答案 0 :(得分:5)

您可以将三种元素类型放在一个选项中,然后将选择按顺序放在另一个序列中。

  <xs:group name="mygr">
    <xs:choice>
      <xs:element name="string"></xs:element>
      <xs:element name="param"></xs:element>
      <xs:element name="switch"></xs:element>
      <xs:element name="env"></xs:element>
    </xs:choice>
  </xs:group>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence >
        <xs:sequence minOccurs="1" maxOccurs="unbounded">
          <xs:group ref="mygr"/>
        </xs:sequence>
        <xs:element name="output"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

这是你已经拥有的,但输出更高。