简体   繁体   English

如何在XSD中限制元素的属性?

[英]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: 另外两项意见:

  1. You can shorten empty elements via <el/> notation. 您可以通过<el/>表示法缩短空元素。
  2. In addition to allowing a student root element, your XSD would allow name , fname , and lname root elements. 除了允许student根元素之外,您的XSD还允许使用namefnamelname根元素。 This doesn't look right. 这看起来不对。 Perhaps you should define all or some of those locally rather than globally. 也许您应该在本地而不是全局定义全部或部分内容。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM