XSD 选择序列
我想编写一个接受这样的 XML 文档的模式:
<plugin>
<label text="blabla" />
<line />
<break />
<textbox name="mytextbox" length="8" required="true" />
<checkbox name="mycheckbox" checked="true" />
<combobox name="mycombo">
<option value="one">Option One</option>
<option value="two" selected="true">Option One</option>
</combobox>
<break />
</plugin>
所以我希望插件包含集合 {combobox,checkbox,textbox,label,line,break} 的元素。 我已经编写了这个 XSD,但这是错误的:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="plugin">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="line" />
<xs:element name="break" />
<xs:element name="label">
<xs:complexType>
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="bold" type="xs:boolean" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
<xs:element name="textbox">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="length" type="xs:positiveInteger" />
<xs:attribute name="required" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="combobox">
<xs:complexType>
<xs:sequence>
<xs:element name="option" nillable="true" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="option_Text" msdata:Ordinal="2">
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string" />
<xs:attribute name="selected" type="xs:boolean" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
<xs:element name="checkbox">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="checked" type="xs:boolean" />
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="plugin" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我已经使用这个验证器工具对其进行了测试...
但它说:
“cvc-complex-type.2.4.d:发现无效内容开头 元素“线”。此时不需要子元素。”
那么...出了什么问题?我不明白此消息。什么时候有什么子元素?
I want to write a schema which accept XML documents like this:
<plugin>
<label text="blabla" />
<line />
<break />
<textbox name="mytextbox" length="8" required="true" />
<checkbox name="mycheckbox" checked="true" />
<combobox name="mycombo">
<option value="one">Option One</option>
<option value="two" selected="true">Option One</option>
</combobox>
<break />
</plugin>
So I want the plugin to contain elements of set {combobox,checkbox,textbox,label,line,break}.
I have written this XSD, but this is wrong:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="plugin">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="line" />
<xs:element name="break" />
<xs:element name="label">
<xs:complexType>
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="bold" type="xs:boolean" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
<xs:element name="textbox">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="length" type="xs:positiveInteger" />
<xs:attribute name="required" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="combobox">
<xs:complexType>
<xs:sequence>
<xs:element name="option" nillable="true" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="option_Text" msdata:Ordinal="2">
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string" />
<xs:attribute name="selected" type="xs:boolean" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
<xs:element name="checkbox">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="checked" type="xs:boolean" />
<xs:attribute name="text" type="xs:string" />
<xs:attribute name="width" type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="plugin" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I have tested it with this validator tool...
but it says:
"cvc-complex-type.2.4.d: Invalid content was found starting with
element 'line'. No child element is expected at this point."
So...What's wrong? I don't understand this message. What child elements at what point?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
sequence
定义只有一个子项,即choice
。这意味着
plugin
只允许有一个子元素,尽管它可以是您定义的任何一个元素。如果删除
choice
元素,将其内容保留在原处,您将拥有一个固定的元素序列,这些元素可以是plugin
的子元素。相反,如果您删除序列元素,将其内容保留在原位,则向
choice
元素添加一个属性:maxOccurs="unbounded"
,那么它应该以任意顺序验证具有您指定的任意数量的子元素的plugin
元素。Your
sequence
definition only has one child, i.e.choice
.This means that
plugin
is only permitted one child, though it may be any one of the elements you have defined.If you remove the
choice
element, leaving its content in situ, you will have a fixed sequence of elements that can be children ofplugin
.If, instead, you remove the sequence element, leaving it's content in situ, and add an attribute to the
choice
element:maxOccurs="unbounded"
, then it should validate aplugin
element with any number of the children you specify, in any order.这将允许您在序列中一遍又一遍地重复选择:
This will allow you to repeat the choice over and over in your sequence: