Detailed introduction to Schema code in XML (picture)

黄舟
Release: 2017-03-10 19:19:22
Original
1545 people have browsed it

XML has the Schema feature, which can introduce element structure more powerfully than DTD. Let's explain in detail the concept, function and usage of Schema in XML. Friends in need can refer to the following

Document Definition The model provides specifications for XML documents. Although the introduction of DTD solves the standardization problem of XML documents, its file format type is inconsistent with the XML file format type. At the same time, the data types provided by DTD are limited and sometimes cannot meet the needs of the industry, so it is introduced. Schema. Schema mode can determine the structure of elements and attributes of an XML document, the order of elements, the data values ​​of elements and attributes, according to ranges, enumerations, and style matching.
Detailed introduction to Schema code in XML (picture)

1. First introduction to Schema

XML Schema language is also called XML Schema Definition (XSD). Its function is to define an A group of legal components of an XML document (the structure of an XML document), just like a DTD. XML Schema is based on XML language. It can also be said that XML Schema itself is an application of XML.

1. The role of Schema

XML Schema has the same role as DTD. They are both used to define the structure of an XML document. So why do we need to What about XML Schema? Because XML Schema is more powerful than DTD.

The advantages of Xml Schema over DTD:
(1) The schema is extensible
(2) The schema is richer and more useful than DTD
(3) The schema uses XML Written
(4) The schema supports data types
(5) The schema supports namespaces
(6) No need to learn other languages ​​
(7) You can directly use the XML editor to write XML Schema
(8) You can directly use an XML parser to parse XML Schema
(9) You can use XML DOM to flexibly operate XML Schema
(10) You can use XSLT technology to convert XML Schema

2. Comparative study

1.1 Function

Both are the same, both are patterns that define the structure of an XML document.

1.2 Usage

Both have the same function, but there are some differences in syntax. Schema is an extension of DTD. It also supports the definition of elements and attributes, and the definition syntax is similar. However, Schema can add corresponding data types to elements and attributes. It also introduces the syntax of global and local element declarations. In addition, according to the elements and attributes The data content introduces simple types and complex types.
The so-called simple types (SimpleType) and complex types (ComplexType) are not specific data types themselves. They are just abstract descriptions of the types of nodes or custom types.

In other words, the introduction of Schema makes the declaration of schema more similar to the programming language we use.

2. Detailed Example

2.1 Schema Example

Listing 1: User.xml document structure

XML/HTML Code复制内容到剪贴板
<?xml version="1.0"?>
<用户列表>
    <用户>
         <用户名>xx</用户名>
         <密码>123456</密码>
         <用户类型>1</用户类型>
   </用户>
</用户列表>
Copy after login


##Listing 2: Using global component form to define Schema, User.xsd

XML/HTML Code复制内容到剪贴板
<!-- 使用全局组件形式定义 -->
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<xs:schema xmlns:xs=&#39;http://www.w3.org/2001/XMLSchema&#39; elementFormDefault=&#39;qualified&#39; attributeFormDefualt=&#39;unqualified&#39;>
 <xs:element name=&#39;用户列表&#39; type=&#39;userlist&#39;/>
 <xs:complexType name=&#39;userlist&#39;><!-- 使用complexType声明该类型为复合类型的元素 -->
  <xs:sequence><!-- 使用sequence说明下面的元素必须按顺序在XML文档中显示 -->
   <xs:element name=&#39;用户&#39; type=&#39;user&#39;/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name=&#39;user&#39;>
  <xs:sequence>
   <xs:element name=&#39;用户名&#39; type=&#39;user&#39;/>
   <xs:element name=&#39;密码&#39; type=&#39;user&#39;/>
   <xs:element name=&#39;用户类型&#39; type=&#39;user&#39;/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>
Copy after login


# #Listing 3: Define Schema using partial form, User.xsd

XML/HTML Code复制内容到剪贴板
<!-- 使用局部形式定义 -->
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<xs:schema xmlns:xs=&#39;http://www.nishishui.org/2000/XML Schema&#39; elementFormDefault=&#39;qualified&#39; attributeFormDefualt=&#39;unqualified&#39;>
 <xs:element name=&#39;用户列表&#39;>
  <xs:complexType>
   <xs:sequence>
    <xs:element name=&#39;用户&#39;>
     <xs:complexType>
      <xs:sequence>
       <xs:element name=&#39;用户名&#39; type=&#39;xs:string&#39;/>
       <xs:element name=&#39;密码&#39; type=&#39;xs:string&#39;/>
       <xs:element name=&#39;用户类型&#39; type=&#39;xs:integer&#39;/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>
Copy after login


##Both Listing 2 and Listing 3 define User. The formal definition of the component, and the partial formal definition used in Listing 3. Please see below for the specific differences.

2.2 Basic Usage


Above we have compared Schema and DTD in terms of function and usage. The biggest difference between Schema and DTD is that Schema Data types are introduced, and other elements, such as the declaration of elements and attributes, are similar to DTD and will not be discussed in detail below.

Schema basic content map:


Detailed introduction to Schema code in XML (picture)#2.2.1 Reference syntax

After a schema file is created, you can use it to Verify the validity of an XML document, that is, check whether an XML document follows the definition of the schema file. So, how does an XML document reference a schema document? The reference of the Schema model is more similar to the application method of the namespace mentioned earlier. The specific examples are as follows:

XML/HTML Code复制内容到剪贴板
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<用户列表 xmlns:xsi=http://www.nishishui.org/2000/XMLSchema xsi:noNamespaceSchemaLocation=&#39;user.xsd&#39;><!-- 引用user.xsd -->
 <用户>
  <用户名>我是谁</用户名>
  <密码>123456</密码>
  <用户类型>1</用户类型>
 </用户>
</用户列表>
Copy after login


#2.2.2 Element type

(1) According to different contents, they are divided into simple and complex elements, which are used respectively simpleType and complexType labels.

Simple element: The content in the element can only be text, and does not contain other elements and attributes.

XML/HTML Code复制内容到剪贴板
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<xs:schema xmlns:xs=&#39;http://www.nishishui.org/2000/XML Schema&#39; elementFormDefault=&#39;qualified&#39; attributeFormDefualt=&#39;unqualified&#39;>
    <xs:element name=&#39;age&#39;>
        <xs:simpleType><!-- 使用关键字simpleType声明简单元素 -->
            <!--restriction关键字结合minInclusive和maxInclusive控制了XML中元素可接受的值范围为0~100-->
            <xs:restriction base="xs:integer">
                <xs:minInclusive value=&#39;0&#39;/>
                <xs:maxInclusive value=&#39;100&#39;/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
</xs:schema>
Copy after login



  • ## Complex elements: elements Contains other elements and attributes. It has four types, namely empty elements, containing only other elements, containing only text, and containing text and other elements.
XML/HTML Code复制内容到剪贴板
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<xs:schema xmlns:xs=&#39;http://www.nishishui.org/2000/XML Schema&#39; elementFormDefault=&#39;qualified&#39; attributeFormDefualt=&#39;unqualified&#39;>
    <xs:element name=&#39;age&#39;>
        <xs:complexType><!-- 使用关键字complexType声明复杂元素 -->
            <!--sequence控制XML内容中元素出现的顺序-->
            <xs:sequence>
                <!-- 定义具体的元素,这些都是简单元素-->
                <xs:element name=&#39;firstname&#39; type=&#39;xs:string&#39;/>
                <xs:element name=&#39;lastname&#39; type=&#39;xs:string&#39;/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
Copy after login

  • ##

    (2)按照定义位置可分为局部和全局元素。
    全局元素:元素的父元素必须是
    局部元素:局部元素声明只能出现在复杂类型(元素)定义内部。即元素的父元素只能是元素。

    XML/HTML Code复制内容到剪贴板
    <?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
    <xs:schema xmlns:xs=&#39;http://www.nishishui.org/2000/XML Schema&#39; elementFormDefault=&#39;qualified&#39; attributeFormDefualt=&#39;unqualified&#39;>
        <xs:element name=&#39;用户&#39; type=&#39;user&#39;/><!-- 全局元素 -->
        <xs:element name=&#39;用户名&#39; type=&#39;xs:string&#39;/><!-- 全局元素 -->
        <xs:element name=&#39;密码&#39; type=&#39;xs:string&#39;><!-- 全局元素 -->
            <xs:complexType name=&#39;user&#39;>
                <!--sequence控制XML内容中元素出现的顺序-->
                <xs:sequence>
                    <!-- 定义具体的元素,这些都是简单元素-->
                    <!-- 定义局部元素,使用ref关键字引用,并使用minOccurs和maxOccurs制定元素出现的最少和最多的次数-->
                    <xs:element ref=&#39;用户名&#39; minOccurs=&#39;0&#39; maxOccurs=&#39;1&#39;/><!-- 局部元素-->
                    <xs:element ref=&#39;密码&#39; minOccurs=&#39;0&#39; maxOccurs=&#39;1&#39;/><!-- 局部元素-->
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    Copy after login


    The above is the detailed content of Detailed introduction to Schema code in XML (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
xml
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!