Detailed introduction to XML and Schema namespaces

黄舟
Release: 2017-04-01 13:51:25
Original
1667 people have browsed it

XMLand 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 itsnamespaceissue, which is detailed in this blog Understand.

##Namespace is a unified naming of elements and ## provided byW3Crecommended 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

DTDshould 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 thestringis 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 inSchema

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


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

 
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 attributedefinitions. As follows:

 
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:

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

  Tom 12 male 
Copy after login

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

  Tom 12 male 
Copy after login

(3)嵌套的命名空间

  Tom 12 male  // 此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。
Copy after login

(4)schemaLocation

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

Copy after login

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

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

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

 John 28 59 .... 
Copy after login

(5)noNamespaceSchemaLocation

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

Copy after login

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

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

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

   Java in action Java is good Bruce  
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!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!