XSD - 任意顺序和计数的元素(XERCES 使用 XSD 验证 XML)
我对 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>
我可以只使用 sequence
、all 或
choice
,但没有一个满足我的要求。序列 - 按精确顺序任意次数。全部 - 以任何顺序零次或一次。选择——只有其中之一。我在这个网站上找到了一个解决方案,但它不适用于 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”不能出现在元素“选择”中
我浏览了互联网,但仍然没有找到我要找的东西。
I have problem with XML schema. I need inside one element elements of three types but without any other restriction, followed by exactly one occurrence of element 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>
I can use just sequence
, all
or choice
, but no one of them satisfies my requirement. Sequence - any number of times in exact order. All - zero or one times in any order. Choice - only one of them. I have found one solution on this web, but it does not work with Xerces. I try this:
<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>
But I get the error: Invalid content was found starting with element 'switch'. One of '{param, output}' is expected. There is a trick.
If
maxOccurs="unbounded"
is moved
from elements to elementchoice
in
schema, then can any of elements
appear in any order in any number of
times.
However, when I do it, I get the error: Attribute 'maxOccurs' cannot appear in element 'choice'
I cruised cross the Internet, but I still haven't found what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将三种元素类型放入一个选项中,然后将该选项放入另一个序列中的一个序列中。
这是您已经拥有的,但输出更高一个级别。
You can put the three element types in a choice and then put the choice in a sequence in another sequence.
Which is what you already had, but the output is taken a level higher.