Home > Article > Backend Development > Detailed introduction to Schema code in XML (picture)
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.
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</用户类型> </用户> </用户列表>
XML/HTML Code复制内容到剪贴板 <!-- 使用全局组件形式定义 --> <?xml version='1.0' encoding='utf-8'?> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefualt='unqualified'> <xs:element name='用户列表' type='userlist'/> <xs:complexType name='userlist'><!-- 使用complexType声明该类型为复合类型的元素 --> <xs:sequence><!-- 使用sequence说明下面的元素必须按顺序在XML文档中显示 --> <xs:element name='用户' type='user'/> </xs:sequence> </xs:complexType> <xs:complexType name='user'> <xs:sequence> <xs:element name='用户名' type='user'/> <xs:element name='密码' type='user'/> <xs:element name='用户类型' type='user'/> </xs:sequence> </xs:complexType> </xs:schema>
# #Listing 3: Define Schema using partial form, User.xsd
XML/HTML Code复制内容到剪贴板 <!-- 使用局部形式定义 --> <?xml version='1.0' encoding='utf-8'?> <xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'> <xs:element name='用户列表'> <xs:complexType> <xs:sequence> <xs:element name='用户'> <xs:complexType> <xs:sequence> <xs:element name='用户名' type='xs:string'/> <xs:element name='密码' type='xs:string'/> <xs:element name='用户类型' type='xs:integer'/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
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.
#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='1.0' encoding='utf-8'?>
<用户列表 xmlns:xsi=http://www.nishishui.org/2000/XMLSchema xsi:noNamespaceSchemaLocation='user.xsd'><!-- 引用user.xsd -->
<用户>
<用户名>我是谁</用户名>
<密码>123456</密码>
<用户类型>1</用户类型>
</用户>
</用户列表>
Simple element: The content in the element can only be text, and does not contain other elements and attributes.
XML/HTML Code复制内容到剪贴板
<?xml version='1.0' encoding='utf-8'?>
<xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>
<xs:element name='age'>
<xs:simpleType><!-- 使用关键字simpleType声明简单元素 -->
<!--restriction关键字结合minInclusive和maxInclusive控制了XML中元素可接受的值范围为0~100-->
<xs:restriction base="xs:integer">
<xs:minInclusive value='0'/>
<xs:maxInclusive value='100'/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
XML/HTML Code复制内容到剪贴板 <?xml version='1.0' encoding='utf-8'?> <xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'> <xs:element name='age'> <xs:complexType><!-- 使用关键字complexType声明复杂元素 --> <!--sequence控制XML内容中元素出现的顺序--> <xs:sequence> <!-- 定义具体的元素,这些都是简单元素--> <xs:element name='firstname' type='xs:string'/> <xs:element name='lastname' type='xs:string'/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
##
(2)按照定义位置可分为局部和全局元素。
全局元素:1dd643d91c792abd0a34d1b312e2f207元素的父元素必须是b7b165c079dc80a737996dfae2b9c338;
局部元素:局部元素声明只能出现在复杂类型(b04708ad431d182f917fc0425233bdde元素)定义内部。即1dd643d91c792abd0a34d1b312e2f207元素的父元素只能是6e9388779e5fc4a565f66fe84a63ec67、6352600d7af58d108c255b7cc4da5797或72b96a4fd26a14b531b12ddf122686e3元素。
XML/HTML Code复制内容到剪贴板 <?xml version='1.0' encoding='utf-8'?> <xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'> <xs:element name='用户' type='user'/><!-- 全局元素 --> <xs:element name='用户名' type='xs:string'/><!-- 全局元素 --> <xs:element name='密码' type='xs:string'><!-- 全局元素 --> <xs:complexType name='user'> <!--sequence控制XML内容中元素出现的顺序--> <xs:sequence> <!-- 定义具体的元素,这些都是简单元素--> <!-- 定义局部元素,使用ref关键字引用,并使用minOccurs和maxOccurs制定元素出现的最少和最多的次数--> <xs:element ref='用户名' minOccurs='0' maxOccurs='1'/><!-- 局部元素--> <xs:element ref='密码' minOccurs='0' maxOccurs='1'/><!-- 局部元素--> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
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!