Home  >  Article  >  Backend Development  >  Detailed explanation of example code for Schema verification XML concept

Detailed explanation of example code for Schema verification XML concept

黄舟
黄舟Original
2018-05-28 10:47:392093browse

1.The structure of the Schema file
The Schema file looks very similar to other XML files. It is composed of a set of elements, and its root element is "Schema ". The "Schema" element is the first element that appears in XML Schema and is used to indicate that the XML document is a Schema document. Correspondingly, the end tag of "Schema" is generally at the end of the document. In this way, the structure of a Schema is as follows:

<Schema name="schema-name" xmlns="namespace" >

</Schema>

Schema has two attributes: name specifies the name of the Schema, and xmlns specifies the namespace contained in the Schema. Note that an XML Schema document can contain multiple namespaces. For example, the following statement specifies three namespaces:

<Schema name="mySchema" 
        xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:myNS=http://www.xml_step_by_step.edu\ns.xml
>

The first one is xmlns="urn:schemas-microsoft-com:xml-data ", which specifies that this document is an XML Schema document; the second is xmlns:dt="urn:schemas-microsoft-com:datatypes", which defines the data types that can be used in this document ;The third one is xmlns:myNS="http://www.xml_step_by_step.edu\ns.xml", which indicates that elements or attributes defined in myNS may be used below.
2.Use Schema to define elements and their contents
Before describing the various definition methods in Schema that correspond to ETD definitions, let’s first take a look at the three Schema elements involved: ElementType ,element,group.
1.ElementType element
Use ElementType in the Schema document to declare the elements that will appear in the XML file. The syntax of ElementType is as follows:

<ElementType 
    name="元素名" 
    content="{ empty | textOnly | eltOnly | mixed }" 
    dt:type="元素类型" 
    order="{ one | seq | many }" 
    model="{ open | closed }" 
>

Among the several attributes of ElementType, the meaning of name is self-evident, it is the name of the declared element, and it is indispensable. content is an important attribute of ElementType. It indicates whether the element declared by ElementType is empty, contains text, contains sub-elements, or contains both text and sub-elements. dt:typeSpecifies the data type of the element. orderSpecify the ordering rules for the child elements of this element. Finally, model specifies whether the element can contain elements and attributes not defined in this Schema. It is mainly used for the introduction of other Schema, that is, the introduction of other "namespaces". The concept of "namespace" may still be unfamiliar to you. Now you only need to understand that several different DTD or Schema definitions can be used in an XML file at the same time.
The following table lists the possible values ​​of model:

Values

Interpretation Meaning

open Indicates that the element can contain other elements and attributes not defined in XML Schema
closed Indicates that the element can only contain elements and attributes defined in this XML Schema

缺省状态下,XML Schema的model取值"open",也就是说,该元素可以包含其它未在XML Schema中定义的元素和属性。但是,这并不意味着任何元素和属性都可在Schema中出现,允许出现的前提是这些"异类"元素和属性必须在单独的XML Schema中加以定义,并且必须在引用的它们的元素中以命名空间形式指定其出处。
      2. element元素 
       ElementType只是起到声明元素的作用,至于元素的内容究竟是什么,则要靠它的子元素element来说明。element的语法表达如下:  

<element 
    type="元素类型" 
    [minOccurs="{ 0 | 1 }"] 
    [maxOccurs="{ 1 | * }"] 
>

element实际上是对该Schema中ElementType声明的引用,而具体引用什么元素类型,就要靠type属性指定了。type属性不可缺少,并且为了保证type指定的是已经声明过的元素,要求它的取值必须同某个ElementType中的name属性严格一致。至于其它两个属性倒是可有可无。minOccurs指定该元素在其父元素中出现的最小次数,缺省值为1,表明该元素至少出现一次;也可以取值为0,表明该元素是可选的,可以不出现。maxOccurs则指定了该元素出现的最大次数,缺省值同样为1,表明该元素至多出现一次;也可取值为“*”,表明该元素在XML实例文档中出现次数不受限制。

    3.group元素 
   DTD中有成组的概念,相应的,Schema中也有“group”元素。它的语法表达类似element元素:

<group
    order="{one | seq | many}" 
    [minOccurs="{ 0 | 1 }"] 
    [maxOccurs="{ 1 | * }"] 
>

Detailed explanation of example code for Schema verification XML concept<AttributeType 和DTD的规定相同,组里的内容可以是元素,也可以是另一个子组。属性order指定该组中的元素或子组的顺序,minOccursmaxOccurs分别指定了该组在其父元素中出现的最小次数和最大次数。

4.用Schema定义元素属性   
    
Schema中用来定义属性的元素有两个,AttributeType元素是声明属性的,attribute元素则是说明一个元素中究竟包含那些属性。 
    AttributeType元素 
   AttributeType元素也是Schema中的重要元素之一,用于定义该Schema文档中出现的属性类型。AttributeType的语法表达如下:

  name="属性名" 
  dt:type="属性类型" 
  dt:values="枚举值列表" 
  default="缺省值" 
  required="{yes | no}" 
>
  1. name
    name不言而喻,自然是所声明的属性类型的名称。注意,该属性是必须的。

  2. dt:type
    dt:type指定所声明属性的数据类型,它除了支持DTD中包含的全部十大数据类型外,还支持一些扩展属性。Schema中的十个基本属性与DTD中属性的对应关系请见下表:

Schema中基本类型 DTD中数据类型
string #PCDATA
enumeration ENUMERATED
id ID
idref IDREF
idrefs IDREFS
nmtoken NMTOKEN
nmtokens NMTOKENS
entity ENTITY
entities EMTITIES
notation NOTATION

3.dt:value
dt:value is only valid when dt:type takes the value "enumeration". At this time, dt:value needs to list all possible values.

4.default
default specifies the default value of this attribute type. The default value must be valid. For example, when dt:type takes the value "enumeration", the default value must come from the values ​​listed in dt:value.

5.required
required specifies whether the attribute is required for the element that references it. A value of yes indicates that it is required, and a value of no indicates that it is not required.

6.Attribute element
The relationship between AttributeType and attribute is the same as that between ElementType and element. AttributeType only serves to declare attributes, but it is not necessary to truly specify which attributes an element has. Rely on the attribute element.

The above is the detailed content of Detailed explanation of example code for Schema verification XML concept. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn