【问题标题】:XSD - Elements in any order and count (XML validated by XERCES using XSD)XSD - 任何顺序和计数的元素(XERCES 使用 XSD 验证的 XML)
【发布时间】:2011-02-23 08:04:50
【问题描述】:

我对 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 in 架构,那么任何元素都可以 以任意数量的任意顺序出现 次。

但是,当我这样做时,我得到了错误:属性“maxOccurs”不能出现在元素“选择”中

我浏览了互联网,但仍然没有找到我要找的东西。

【问题讨论】:

    标签: xml xsd xerces


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多