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>
## 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='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)按照定义位置可分为局部和全局元素。
全局元素:元素的父元素必须是 ;
局部元素:局部元素声明只能出现在复杂类型(元素)定义内部。即 元素的父元素只能是 、 或 元素。 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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
