XML: How to place a restriction to a base simple element which was extended with attributes?
I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child name="MyType" compareMode="EQ">Child1</Child>
</Root>
Usually in orde开发者_如何学Cr to verify such an xml one would use the following xml schema:
<?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>
I want to limit values of a Child element and allow only the following: Child1, Child2 and Child3.
I know that usually restriction may be specified with the following schema:
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
Which restriction is correct in the first case?
Does this answer your question?
Having both an attribute and a restriction on an element in xml schema
I was wrong, because I didn't test my answer. I edited it:
<?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>
or with restriction:
<?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>
After hours spent with google: You cannot do this without named types.
精彩评论