


Specific example code sharing of XML and Schema namespaces
The above describes XML and SchemaSome related contents of the namespace are introduced in detail. Let’s learn more about it through examples:
Example 1: Key points to understand Related concepts of namespace
The following example is an XML Schema file named "note.xsd"
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xs:string"/> <xsd:element name="from" type="xs:string"/> <xsd:element name="heading" type="xs:string"/> <xsd:element name="body" type="xs:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
The XML document below and the one given above The XML Schema is associated with it, named "note.xml", and the following discussion will focus on these two documents
. #<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This fragment: xmlns:xsd="
www.w3.org/2001/XMLSchema", indicating that this schema is used The elements and datatypes come from the "www.w3.org/2001/XMLSchema" namespace (it also indicates that they come from "www.w3.org". /2001/XMLSchema"Namespace elements and data types must be used with "xsd:
" prefix. As a namespace identifier (in a declaration as a prefix to an element or attribute), you can also use it without xsd or xsi. This xmlns attribute contains basic XML schema elements, such as element, attribute, complexType, group, simpleType, etc. ## For any XML Schema definition document (XSD), there is a top-level schema (XSD) element. The definition must include this namespace:
. That is, this namespace is the standard namespace defined by the XML Schema specification - all XML Schema elements must belong to this namespace ##. # This fragment: targetNamespace="www.w3schools.com" indicates this schema
The elements defined by (note, to, from, heading, body) come from the "www.w3schools.com
" namespace. The targetNamespace attribute represents the URI of the namespace corresponding to the schema. That is to say, the namespace must be declared in other documents (including its own document) that reference the Schema, and its URI should be the attribute value of targetNamespace. For example, here we need to use the extended data type defined by note.xsd (note,
to, from, heading, body), so the namespace xmlns="www.w3schools.com" is also declared. And that namespace is the default namespace (no prefix). The targetNamespace attribute declares an XML namespace for all new types explicitly created in the schema. Let’s look at what the beginning of the XML document note.xml specified by this schema will look like:
其中缺省名称空间声明xmlns="www.w3schools.com"就是和刚刚声明的XML
Schema的名称空间相结合来规定该XML文档。(即该文档用到了此名称空间中定义的数据) xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
是任何XML实例文档固有的XML模式实例名称空间,它由XML模式规范定义。而xsi:schemaLocation="www.w3schools.com note.xsd"则规定了该名称空间所对应的schema的位置,即在相同路径的note.xsd文件。
xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。修改一下note.xsd,去除默认名称空间的声明,并添加一个复杂类型: 上述代码中,复杂类型stu是找不到的,因为你定义了一个名称空间"www.w3schools.com",该复杂类型存在于"www.w3schools.com"中,因此应该修改代码如下:<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
例二:重点理解Schema文档使用自身定义类型
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns:student="http://www.w3schools.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xs:string"/>
<xsd:element name="from" type="xs:string"/>
<xsd:element name="heading" type="xs:string"/>
<xsd:element name="body" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student" type="student:stu"/>
<xsd:complexType name="stu">
<xsd:sequence>
<xsd:element name="Name" type="xs:string"/>
<xsd:element name="Class" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。
通过上面的例子,我们可以很深刻的理解targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而"www.w3.org/2001/XMLSchema"名称空间则定义了element,
attribute, complexType, group, simpleType等元素。
理解了上面的两个例子,Schema的命名空间的内容应该就明了了。
The above is the detailed content of Specific example code sharing of XML and Schema namespaces. 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

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

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.

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