Home >Backend Development >XML/RSS Tutorial >Detailed sample code explanation of namespaces in XML

Detailed sample code explanation of namespaces in XML

黄舟
黄舟Original
2017-03-10 19:24:011514browse

This article mainly introduces the namespace of XML, including the basic knowledge of XML introductory learning such as namespace declaration. Friends in need can refer to it

A namespace is a set of unique names. Namespaces are a mechanism for determining which element and attribute names can be assigned to a certain group. Namespaces are identified by URIs (Uniform Resource Identifiers).

Namespace declaration
Namespace is declared using reserved attributes. Such attribute names must be xmlns or start with xmlns:. It looks like this:

<element xmlns:name="URL">

Syntax
The namespace begins with the keyword xmlns.
name is the namespace prefix.
URL is the namespace identifier.

Example
Namespaces only affect limited areas of the document. The element containing this declaration and all its child elements are within the namespace scope. Here is a simple example of an XML namespace:

<?xml version="1.0" encoding="UTF-8"?>  
<cont:contact xmlns:cont="www.tutorialspoint.com/profile">  
    <cont:name>Tanmay Patil</cont:name>  
    <cont:company>TutorialsPoint</cont:company>  
    <cont:phone>(011) 123-4567</cont:phone>  
</cont:contact>

Here, the namespace prefix is ​​cont and the identifier (URI) is www.tutorialspoint.com/profile. This means that element names and attribute names prefixed with cont (including contact elements) belong to the www.tutorialspoint.com/profile namespace.

In XML, the use of namespaces involves the concept of category. Category is the coverage of the namespace. It refers to which elements and attributes are in the namespace and which are not in the namespace. A namespace can limit the entire XML document or only a part of the XML document.
(1) The namespace limits the entire XML document

<span style="font-family:SimSun;font-size:14px;"><?xml version=”1.0”?>     
<member_details xmlns=”http://www.testns.com/ns.xsd”>     
   <name>Tom</name>     
   <age>12</age>     
   <sex>male</sex>     
</member_details></span>

(2) The namespace only targets a part of the XML document

<span style="font-family:SimSun;font-size:14px;"><?xml version=”1.0”?>     
<member_details>     
   <name xmlns=”http://www.testns.com/ns.xsd”>Tom</name>     
   <age>12</age>     
   <sex>male</sex>     
</member_details></span>

(3) Nested namespace

<span style="font-family:SimSun;font-size:14px;"><?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>     
//<span style="line-height: 26px;"> 此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。</span></span>

The above is the detailed content of Detailed sample code explanation of namespaces in XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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