Home Backend Development XML/RSS Tutorial Detailed introduction to Schema code in XML (picture)

Detailed introduction to Schema code in XML (picture)

Mar 10, 2017 pm 07:19 PM
xml

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

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 Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

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 to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

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

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

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,

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

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 Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

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 Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

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 parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

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

See all articles