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

Detailed sample code explanation of namespaces in XML

Mar 10, 2017 pm 07:24 PM
xml Namespaces

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">
Copy after login

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>
Copy after login

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>
Copy after login

(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>
Copy after login

(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>
Copy after login

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