


Detailed explanation of example code for Schema verification XML concept
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 | * }"] >
<AttributeType 和DTD的规定相同,组里的内容可以是元素,也可以是另一个子组。属性order指定该组中的元素或子组的顺序,minOccurs和maxOccurs分别指定了该组在其父元素中出现的最小次数和最大次数。
4.用Schema定义元素属性
Schema中用来定义属性的元素有两个,AttributeType元素是声明属性的,attribute元素则是说明一个元素中究竟包含那些属性。
AttributeType元素
AttributeType元素也是Schema中的重要元素之一,用于定义该Schema文档中出现的属性类型。AttributeType的语法表达如下:
name="属性名" dt:type="属性类型" dt:values="枚举值列表" default="缺省值" required="{yes | no}" >
name
name不言而喻,自然是所声明的属性类型的名称。注意,该属性是必须的。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!

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
