Table of Contents
(1) Global components in Schema" > (1) Global components in Schema
Global components refer to the direct child nodes of the element xsd:schema, including element declarations, attribute declarations, complex/simple type definitions, group definitions, and attribute group definitions ##. " > Global components refer to the direct child nodes of the element xsd:schema, including element declarations, attribute declarations, complex/simple type definitions, group definitions, and attribute group definitions ##.
     3、XML文档中命名空间" >     3、XML文档中命名空间
     (1).名称空间限定整个XML文档" >     (1).名称空间限定整个XML文档
     (2)名称空间只针对XML文档中的一部分" >     (2)名称空间只针对XML文档中的一部分
     (3)嵌套的命名空间" >     (3)嵌套的命名空间
     (4)schemaLocation" >     (4)schemaLocation
   (5)noNamespaceSchemaLocation " >   (5)noNamespaceSchemaLocation 
Home Backend Development XML/RSS Tutorial Detailed introduction to XML and Schema namespaces

Detailed introduction to XML and Schema namespaces

Apr 01, 2017 pm 01:51 PM

XML and Schema have the characteristics of being independent of platforms, technology vendors, simplicity, and unified specifications. They are extremely open, so they are widely used and easy to use. In XML and Schema, personal One thing that I think is difficult to understand is itsnamespace issue, which is detailed in this blog Understand.

##Namespace is a unified naming of elements and ## provided by W3Crecommended standards. #Mechanism of attributes. Use namespaces to clearly identify and combine elements and attributes from different markup vocabularies in XML documents, avoiding name conflicts.

## People who have used

DTD should know that naming conflicts are a major problem in DTD, and the introduction of the concept of namespace in Schema solves this problem very well. Specifically:

1.

Declare the namespace

The general form of a namespace declaration is: the first part is a keyword xmlns:, the second part is the prefix of the namespace, the third part is an equal sign, the fourth part is double quotes, and the fifth part is The namespace identifier URI is included. It should be noted that the prefix of the namespace cannot be xml, because the string is reserved for special purposes in XML. Example: ##. #

xmlns:tns="http://www.whtest.com/"    
//其中tns为前缀。
Copy after login

## You can also declare the namespace implicitly, that is, omit the colon and namespace prefix. Example:

##

xmlns="http://www.whtest.com/"   //注意在一个文档中只能有一个隐式声明的命名空间
Copy after login

## 2. Schema Namespace in:

(1) Global components in Schema

Global components refer to the direct child nodes of the element xsd:schema, including element declarations, attribute declarations, complex/simple type definitions, group definitions, and attribute group definitions ##.


<?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema”
targetNamespace=“http://www.test.com/ns/ns_test“>
   // Schema的目标名称空间用属性targetNamespace在根元素上定义。
   //Schema的全局成分被放在名称空间http://www.test.com/ns/ns_test里。
Copy after login

(2)Non-global components in

Schema

## Sometimes you want to define non-global components in the target space, you can use the following method

<?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema”
targetNamespace=“http://www.test.com/ns/ns_test“
elementFormDefault=“qualified“>
Copy after login

The default value of the attribute elementFormDefault is unqualified, which stipulates that only global components are defined in the target name in space. Assign the value of elementFormDefault to qualified so that the target namespace contains non-global element definitions. Similarly, assigning the value of attributeFormDefault to qualified allows the target namespace to contain non-global attribute definitions. As follows:

<?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema”
targetNamespace=“http://www.test.com/ns/ns_test“
attributeFormDefault=“qualified“>
Copy after login

You can also modify the value of the attribute form so that some non-global components are not included in the name space. as follows:

<xsd:element name=”name” type=”xsd:string” form=”unqualified”/>
Copy after login

(3)targetNamespace

xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。

targetNamespace定义了Schema定义的新元素与属性的名称空间。而"www.w3.org/2001/XMLSchema"名称空间则定义了element, attribute, complexType, group, simpleType等元素。

若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。

3、XML文档中命名空间

在XML中,名称空间的使用涉及范畴的概念,范畴即名称空间的覆盖范围,它指的是哪些元素和属性在该名称空间中,哪些不在该名称空间中。名称空间既可以限定整个XML文档,也可以只针对XML文档中的一部分。

(1).名称空间限定整个XML文档

<?xml version=”1.0”?>
<member_details xmlns=”http://www.testns.com/ns.xsd”>
   <name>Tom</name>
   <age>12</age>
   <sex>male</sex>
</member_details>
Copy after login

(2)名称空间只针对XML文档中的一部分

<?xml version=”1.0”?>
<member_details>
   <name xmlns=”http://www.testns.com/ns.xsd”>Tom</name>
   <age>12</age>
   <sex>male</sex>
</member_details>
Copy after login

(3)嵌套的命名空间

<?xml version=”1.0”?>
<member_details xmlns=”http://www.testns.com/ns.xsd”
xmlns:newns=”http://www.testns/newns.xsd”>
   <name>Tom</name>
   <age>12</age>
   <newns:sex>male</sex>
</member_details>
// 此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。
Copy after login

(4)schemaLocation

schemaLocation 属性引用具有目标名称空间的 XML 架构文档(.xsd)。该xml文件中用到的所有新创的元素、属性等的.xsd文件都必须在这里声明。

<xsi:schemaLocation="list of anyURI" >
Copy after login

其中的anyURI是一个架构位置,该架构包含限定的(具有名称空间的架构)架构构造。每一对中的第一个 URI 引用是名称空间名称,第二个则是描述名称空间的架构的位置。

将具有目标名称空间的架构文档与实例文档相关联。可以列出多对 URI 引用,每一对都有不同的名称空间名称部分。
根据万维网联合会 (W3C) XML 架构建议,XML 实例文档可以同时指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 属性。此外,还可以多次列出同一个命名空间。

以下示例显示如何使用 xsi:schemaLocation 属性为多个 XML 架构文档提供位置信息。

<p:Person
   xmlns:p="http://contoso.com/People"
   xmlns:v="http://contoso.com /Vehicles"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
     "http://contoso.com/People 
      http://contoso.com/schemas/people.xsd 
      http://contoso.com/schemas/Vehicles
      http://contoso.com/schemas/vehicles.xsd
      http://contoso.com/schemas/People
      http://contoso.com/schemas/people.xsd">
   <name>John</name>
   <age>28</age>
   <height>59</height>
   ....
</p:Person>
Copy after login

(5)noNamespaceSchemaLocation

noNamespaceSchemaLocation 属性引用没有目标名称空间的 XML 架构文档。

    <xsi:noNamespaceSchemaLocation="anyURI" >
Copy after login

与SchemaLocation相同anyURI是一个架构位置,该架构包含非限定的(没有名称空间的架构)架构构造。

不要求 XML 架构有名称空间。若要为没有目标名称空间的 XML 架构指定位置,请使用 noNamespaceSchemaLocation 属性。此属性中引用的 XML 架构不能有目标名称空间。因为此属性不接受 URL 列表,所以只能指定一个架构位置。
根据万维网联合会 (W3C) XML 架构建议,XML 实例文档可以同时指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 属性。

以下示例显示如何对包含非限定元素的实例文档使用 xsi:noNamespaceSchemaLocation 属性。

<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:book="http://www.example.org/02"
	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:noNamespaceSchemaLocation="02.xsd">
	<book:book id="1">
		<book:title>Java in action</book:title>
		<book:content>Java is good</book:content>
		<book:author>Bruce</book:author>
	</book:book>
</book:books>
Copy after login


     通过上边的分析,我们可以看到,XML和Schema的命名空间标签使用格式是相同的(这也是Schema相对与DTD的优势),但XML和Schema都有各自的独特的属性,这也是由他们不同的功能决定的,Schema主要给XML提供服务,所以会规定好targetNameSpace来声明命名空间的名字,而XML需要使用schema的服务,所以需要SchemaLocation来声明使用的命名空间。

The above is the detailed content of Detailed introduction to XML and Schema namespaces. 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

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

Solve PHP error: The specified namespace class was not found Solve PHP error: The specified namespace class was not found Aug 18, 2023 pm 11:28 PM

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

C++ syntax error: undefined namespace used, how to deal with it? C++ syntax error: undefined namespace used, how to deal with it? Aug 21, 2023 pm 09:49 PM

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

See all articles