Validation XML:如何对使用属性扩展的基本简单元素进行限制?
我有以下xml:Validation XML:如何对使用属性扩展的基本简单元素进行限制?,validation,xsd,restriction,Validation,Xsd,Restriction,我有以下xml: <?xml version="1.0" encoding="utf-8"?> <Root> <Child name="MyType" compareMode="EQ">Child1</Child> </Root> 孩子1 通常,为了验证此类xml,将使用以下xml模式: <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFo
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child name="MyType" compareMode="EQ">Child1</Child>
</Root>
孩子1
通常,为了验证此类xml,将使用以下xml模式:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我想限制子元素的值,只允许以下内容:Child1、Child2和Child3
我知道通常可以使用以下模式指定限制:
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
在第一种情况下,哪种限制是正确的?这是否回答了您的问题
我错了,因为我没有测试我的答案。我编辑了它:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ChildContent">
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="ChildContent">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" type="Child" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
或有限制地:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="Child">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
与谷歌共度数小时后:如果没有命名的类型,你就无法做到这一点。@inf3mo谢谢你的回答!不幸的是,当我应用您的配置时,我看到以下错误:s4s elt无效内容。1:“#AnonType_MetaInfoRootChild”的内容无效。元素“simpleContent”无效、放置错误或出现频率过高。。为了构建这样的xml模式,您使用了哪些信息源?你能删除一个链接吗?+1,这看起来是正确的,事实上,我使用了Tomasz提出的相同解决方案。无论如何谢谢你。是的,我看到太晚了,Tomasz写了相同的解决方案,所以我再次编辑,并在“或有限制”部分下添加了另一个解决方案。