XMLnamespace

LearningXMLnamespace concepts, 语法 and application, Understandsuch as何 in XMLdocumentationin避免命名conflict, implementation不同XML词汇 混合using

XMLnamespaceoverview

in XMLin, 当 many 个不同 XML词汇表 (such asHTML, SVG, MathMLetc.) in 同一个documentationinusing时, 可能会出现元素 or property名称conflict issues. XMLnamespace (XML Namespaces) providing了一种mechanism, 用于区分不同XML词汇表in具 has 相同名称 元素 and property.

命名conflictissues

考虑以 under circumstances: 一个XMLdocumentation同时package含HTML and SVG in 容, 两者都 has <title>元素. 没 has namespace, 解析器无法区分这两个<title>元素分别属于哪个词汇表.

命名conflictexample
<?xml version="1.0" encoding="UTF-8"?>
<document>
    <!-- HTML title元素 -->
    <title>documentation标题</title>
    
    <!-- SVG title元素 -->
    <svg width="100" height="100">
        <title>SVGgraph形标题</title>
        <circle cx="50" cy="50" r="40" fill="red" />
    </svg>
</document>

namespace solution

XMLnamespacethrough for 元素 and property名称添加 before 缀 or 关联 to namespaceURI (Uniform Resource Identifier) 来解决命名conflict. 每个namespace都 has 一个唯一 URI, 用于标识该namespace.

usingnamespace解决conflict
<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns:html="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg">
    <!-- HTML title元素 -->
    <html:title>documentation标题</html:title>
    
    <!-- SVG title元素 -->
    <svg:svg width="100" height="100">
        <svg:title>SVGgraph形标题</svg:title>
        <svg:circle cx="50" cy="50" r="40" fill="red" />
    </svg:svg>
</doc>

XMLnamespace 语法

XMLnamespace可以throughxmlnsproperty or 带 has before 缀 xmlns:prefixproperty来声明.

namespace声明

namespace声明 basic语法 is :

xmlns:prefix="namespace-uri"

其in:

  • xmlns is XMLnamespace 保留property
  • prefix is 可选 namespace before 缀, 用于标识该namespacein 元素 and property
  • namespace-uri is namespace 唯一标识符, 通常 is a URL

默认namespace

such as果省略 before 缀, 则声明 is 默认namespace. 默认namespace适用于所 has 没 has before 缀 元素.

默认namespaceexample
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://example.com/library">
    <book>
        <title>XMLBasicstutorial</title>
        <author>张三</author>
    </book>
</library>

带 before 缀 namespace

带 before 缀 namespace允许 in 同一documentationinusing many 个namespace, 每个namespaceusing不同 before 缀来标识.

带 before 缀 namespaceexample
<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns:lib="http://example.com/library" xmlns:emp="http://example.com/employee">
    <lib:book>
        <lib:title>XMLBasicstutorial</lib:title>
        <lib:author>张三</lib:author>
    </lib:book>
    
    <emp:employee>
        <emp:name>李四</emp:name>
        <emp:department>techniques部</emp:department>
    </emp:employee>
</doc>

namespace 作用域

namespace声明 作用域 from 声明它 元素开始, including该元素 所 has 子元素, 直 to 被另一个同名 before 缀 声明覆盖, or to 达元素 结束tag.

namespace作用域example
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com/default">
    <!-- 属于默认namespace -->
    <element1> in 容</element1>
    
    <!-- 声明 new  namespace, 覆盖默认namespace -->
    <element2 xmlns="http://example.com/new">
        <!-- 属于 new  namespace -->
        <child> in 容</child>
    </element2>
    
    <!-- 回 to 默认namespace -->
    <element3> in 容</element3>
</root>

property namespace

默认circumstances under , property不属于任何namespace, 除非明确 for 其指定 before 缀. 这 is and 元素 一个 important 区别.

propertynamespaceexample

propertynamespaceusingexample
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com/default" xmlns:prefix="http://example.com/prefix">
    <!-- propertyattr1不属于任何namespace -->
    <element attr1="value1"> in 容</element>
    
    <!-- propertyprefix:attr2属于prefixnamespace -->
    <element prefix:attr2="value2"> in 容</element>
</root>

propertynamespace best practices

  • 只 has 当property名称可能 and othernamespacein propertyconflict时, 才 for property指定namespace before 缀
  • 避免 for 同一namespace 元素 and propertyusing不同 before 缀
  • 保持propertynamespace consistency and readable 性

namespaceURI

namespaceURI is namespace 唯一标识符, 通常 is a URL (such ashttp://www.w3.org/2000/svg) , 但也可以 is URN (such asurn:example:namespace) .

namespaceURI 特点

  • namespaceURI不需要指向一个practical存 in 网页 or resource
  • namespaceURI 唯一作用 is providing一个唯一 标识符
  • usingURLserving asnamespaceURI good 处 is 可以利用域名 唯一性
  • namespaceURI is big small 写敏感

common namespaceURI

XML词汇 namespaceURI
HTML http://www.w3.org/1999/xhtml
SVG http://www.w3.org/2000/svg
MathML http://www.w3.org/1998/Math/MathML
XSLT http://www.w3.org/1999/XSL/Transform
XPath http://www.w3.org/1999/XSL/Transform

XMLnamespace practicalapplication

XMLnamespace in 许 many practicalapplicationin被广泛using, 特别 is in 需要混合using不同XML词汇表 场景in.

1. XHTMLdocumentation

XHTML is HTML XML语法version, usingXHTMLnamespace.

XHTMLdocumentationexample
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>XHTMLdocumentation</title>
    </head>
    <body>
        <h1>欢迎usingXHTML</h1>
        <p>这 is a XHTMLdocumentation. </p>
    </body>
</html>

2. SVG嵌入 to HTMLin

当 in HTMLdocumentationin嵌入SVGgraph形时, 需要usingnamespace来区分HTML and SVG元素.

SVG嵌入HTMLexample
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg">
    <head>
        <title>SVG嵌入example</title>
    </head>
    <body>
        <h1>SVGgraph形example</h1>
        <svg:svg width="200" height="200">
            <svg:circle cx="100" cy="100" r="80" fill="red" />
            <svg:text x="100" y="110" text-anchor="middle" fill="white" font-size="24">Hello SVG</svg:text>
        </svg:svg>
    </body>
</html>

3. SOAPmessage

SOAP (Simple Object Access Protocol) is a基于XML protocol, 用于 in network on 交换structure化information. SOAPmessageusing many 个namespace来区分不同 元素.

SOAPmessageexample
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://example.com/weather">
    <soap:Header>
        <m:AuthenticationToken>abc123</m:AuthenticationToken>
    </soap:Header>
    <soap:Body>
        <m:GetWeather>
            <m:City>北京</m:City>
            <m:Date>2025-01-01</m:Date>
        </m:GetWeather>
    </soap:Body>
</soap:Envelope>

实践case: creation带namespace XMLdocumentation

casedescribes

creation一个XMLdocumentation, 用于describes一个package含graph书 and 作者information library, usingnamespace来区分不同class型 information.

implementation步骤

  1. creation一个名 for library_with_namespaces.xml XMLfile
  2. 添加XML声明
  3. 声明两个namespace:
    • 一个用于graph书information: http://example.com/library/books
    • 一个用于作者information: http://example.com/library/authors
  4. creation根元素<library>
  5. in 根元素 in 添加graph书 and 作者information, using相应 namespace before 缀

最终code

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns:book="http://example.com/library/books" xmlns:author="http://example.com/library/authors">
    <book:book id="B001">
        <book:title>XMLBasicstutorial</book:title>
        <book:year>2025</book:year>
        <book:price>99.00</book:price>
        <book:author ref="A001" />
    </book:book>
    
    <book:book id="B002">
        <book:title>XMLadvancedapplication</book:title>
        <book:year>2025</book:year>
        <book:price>129.00</book:price>
        <book:author ref="A001" />
    </book:book>
    
    <author:author id="A001">
        <author:name>张三</author:name>
        <author:email>zhangsan@example.com</author:email>
        <author:bio>资深XML专家, 拥 has 10年XMLDevelopmentexperience</author:bio>
    </author:author>
</library>

互动练习

练习1: 识别namespaceissues

以 under XMLdocumentation存 in 哪些namespace相关issues?
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:lib="http://example.com/library" xmlns:emp="http://example.com/employee">
    <lib:book>
        <title>XMLBasicstutorial</title> <!-- issues1 -->
        <lib:author>张三</lib:author>
    </lib:book>
    
    <emp:employee>
        <emp:name>李四</emp:name>
        <dept>techniques部</dept> <!-- issues2 -->
    </emp:employee>
    
    <book> <!-- issues3 -->
        <title>XMLadvancedapplication</title>
        <author>王五</author>
    </book>
</document>

XMLdocumentation存 in 以 under namespace相关issues:

  1. 第一个<title>元素没 has usinglib: before 缀, 虽然它 is <lib:book> 子元素, 但默认circumstances under 不inheritancenamespace
  2. <dept>元素没 has usingemp: before 缀, and issues1class似
  3. <book>元素没 has using任何 before 缀, 也没 has 声明默认namespace, 因此它不属于任何namespace

练习2: creation带namespace XMLdocumentation

creation一个XMLdocumentation, 用于describes一个package含产品 and 供应商information Table of Contents, using以 under namespace:
  • 产品namespace: http://example.com/catalog/products
  • 供应商namespace: http://example.com/catalog/suppliers
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:prod="http://example.com/catalog/products" xmlns:supp="http://example.com/catalog/suppliers">
    <prod:product id="P001">
        <prod:name>智能手机</prod:name>
        <prod:price>3999</prod:price>
        <prod:supplier ref="S001" />
    </prod:product>
    
    <prod:product id="P002">
        <prod:name>笔记本电脑</prod:name>
        <prod:price>7999</prod:price>
        <prod:supplier ref="S002" />
    </prod:product>
    
    <supp:supplier id="S001">
        <supp:name>科技 has 限公司</supp:name>
        <supp:contact>张经理</supp:contact>
        <supp:phone>13800138000</supp:phone>
    </supp:supplier>
    
    <supp:supplier id="S002">
        <supp:name>电子设备公司</supp:name>
        <supp:contact>李经理</supp:contact>
        <supp:phone>13900139000</supp:phone>
    </supp:supplier>
</catalog>