[英]How to restrict an attribute of an element in XSD?
I have this XML code: 我有这个XML代码:
<student>
<name sex="male">
<fname></fname>
<lname></lname>
</name>
</student>
What I want to do is to add a restriction to the sex
attribute. 我想要做的是为
sex
属性添加限制。 sex
attribute should only take male and female as their value. sex
属性应该只考虑男性和女性的价值。 I have basic knowledge of making this XML Schema. 我有制作这个XML Schema的基本知识。 So far what I've done is this:
到目前为止,我所做的是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="nameType"/>
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="student" type="studentType"/>
<xs:complexType name="nameType">
<xs:sequence>
<xs:element ref="fname"></xs:element>
<xs:element ref="lname"></xs:element>
</xs:sequence>
<xs:attribute name="sex"></xs:attribute>
</xs:complexType>
<xs:complexType name="studentType">
<xs:sequence>
<xs:element ref="name"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
You can add restriction to the value of the attribute as shown below: 您可以对属性的值添加限制,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="nameType"/>
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="student" type="studentType"/>
<xs:complexType name="nameType">
<xs:sequence>
<xs:element ref="fname"></xs:element>
<xs:element ref="lname"></xs:element>
</xs:sequence>
<xs:attribute name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ar"></xs:enumeration>
<xs:enumeration value="en"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="studentType">
<xs:sequence>
<xs:element ref="name"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Use xs:restriction
to limit an attribute to a fixed set of enumerations via xs:enumeration
: 使用
xs:restriction
通过xs:enumeration
将属性限制为一组固定的xs:enumeration
:
<xs:attribute name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male"/>
<xs:enumeration value="female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Two other observations: 另外两项意见:
<el/>
notation. <el/>
表示法缩短空元素。 student
root element, your XSD would allow name
, fname
, and lname
root elements. student
根元素之外,您的XSD还允许使用name
, fname
和lname
根元素。 This doesn't look right. Here is your XSD with all of the above adjustments: 以下是所有上述调整的XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student" type="studentType"/>
<xs:complexType name="studentType">
<xs:sequence>
<xs:element name="name" type="nameType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="nameType">
<xs:sequence>
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
</xs:sequence>
<xs:attribute name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male"/>
<xs:enumeration value="female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.