XML 命名空间

XML命名空间详解
本文介绍了XML命名空间的基本概念,解释了如何使用命名空间避免元素名称冲突,并展示了如何通过定义前缀和默认命名空间来实现这一目标。此外,还提供了一个实际应用场景——XSL样式表中的命名空间使用案例。

XML Namespaces provide a method to avoid element name conflicts.
XML 命名空间提供了避免元素名称冲突的方法。


--------------------------------------------------------------------------------

Name Conflicts
名称冲突
Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.
因为XML的元素名称并不是预先定义的,因此,如果两份不同文档的元素名称相同,就会产生名称冲突。

This XML document carries information in a table:
这份XML文档携带了表格中的信息:

<table>   <tr>   <td>Apples</td>   <td>Bananas</td>   </tr></table>

This XML document carries information about a table (a piece of furniture):
这份XML文档携带了一张桌子的信息(一件家具):

<table>   <name>African Coffee Table</name>   <width>80</width>   <length>120</length></table>

If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.
如果把两份XML文档拼接起来,就会出现元素名称冲突,因为两份文件都包含一个<table>元素,但其所指定的内容和定义是不同的。


--------------------------------------------------------------------------------

Solving Name Conflicts Using a Prefix
用前缀法解决名称冲突盾问题
This XML document carries information in a table:
这份XML文档携带了表格中的信息:

<h:table>   <h:tr>   <h:td>Apples</h:td>   <h:td>Bananas</h:td>   </h:tr></h:table>

This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:

<f:table>   <f:name>African Coffee Table</f:name>   <f:width>80</f:width>   <f:length>120</f:length></f:table>

Now there will be no name conflict because the two documents use a different name for their <table> element (<h:table> and <f:table>).
那么,现在就不会有名称冲突的问题了,因为两分文档的<table>元素使用了不同的名称(<h:table> 和<f:table>)。

By using a prefix, we have created two different types of <table> elements.
通过使用前缀法,我们创建了两种不同类型的<table>元素。


--------------------------------------------------------------------------------

Using Namespaces
使用命名空间
This XML document carries information in a table:
这份XML文档携带了表格中的信息:

<h:table xmlns:h="http://www.w3.org/TR/html4/">   <h:tr>   <h:td>Apples</h:td>   <h:td>Bananas</h:td>   </h:tr></h:table>

This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:

<f:table xmlns:f="http://www.w3schools.com/furniture">   <f:name>African Coffee Table</f:name>   <f:width>80</f:width>   <f:length>120</f:length></f:table>

Instead of using only prefixes, we have added an xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.
在这里,我们并没有使用前缀,取而代之的是,我们给<table>标签添加了一个xmlns属性,使前缀包含了一个与命名空间相匹配的合法名称。


--------------------------------------------------------------------------------

The XML Namespace (xmlns) Attribute
XML名称空间的(xmlns)属性
The XML namespace attribute is placed in the start tag of an element and has the following syntax:
将XML命名空间属性放置在一个元素的起始标签中,语法如下:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
在元素的起始标签中定义一个命名空间时,所有包含相同前缀的子元素都与相同的名称空间相匹配;

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
值得注意的是:解析器并不会使用这些用于定义命名空间的地址来搜索信息。这样做的目的仅是为命名空间提供一个独立的名称。然而,在大多数情况下,公司会将命名空间作为一个指示器,指向包含命名空间具体信息的真实网页。
自己看看吧: http://www.w3.org/TR/html4/.


--------------------------------------------------------------------------------

Uniform Resource Identifier (URI)
统一资源识别器(URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.
URI是识别网络资源的一组字符串。最普通的URI是统一资源定位器(URL),它用于对互联网的域名地址进行定位;另一种不太普遍的URI是通用资源名称 (URN)。在我们的案例中,我们只使用URL。


--------------------------------------------------------------------------------

Default Namespaces
默认的命名空间
Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:
为元素定义一个默认的命名空间,我们就不需要为每个子元素定义前缀。语法如下:

xmlns="namespaceURI"

This XML document carries information in a table:
这份XML文档携带了表格中的信息:

<table xmlns="http://www.w3.org/TR/html4/">   <tr>   <td>Apples</td>   <td>Bananas</td>   </tr></table>

This XML document carries information about a piece of furniture:
这份XML文档中携带了一件家具的信息:

<table xmlns="http://www.w3schools.com/furniture">   <name>African Coffee Table</name>   <width>80</width>   <length>120</length></table>

--------------------------------------------------------------------------------

Namespaces in Real Use
命名空间的实际应用
When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML.
当你开始使用XSL时,你会很快地发现命名空间在现实中的使用方法。XSL式样表用于将XML文档转换成其它格式,如:HTML格式。

If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform":
如果你仔细看看下面的例子,你会发现,大部分标签都是HTML型的标签。非HTML的标签包含了 前缀名“xsl” ,它可以被命名空间"http://www.w3.org/1999/XSL/Transform"所识别:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body>  <h2>My CD Collection</h2>  <table border="1">    <tr>      <th align="left">Title</th>      <th align="left">Artist</th>    </tr>    <xsl:for-each select="catalog/cd">    <tr>      <td><xsl:value-of select="title"/></td>      <td><xsl:value-of select="artist"/></td>    </tr>    </xsl:for-each>  </table></body></html></xsl:template></xsl:stylesheet>
 

采用PyQt5框架与Python编程语言构建图书信息管理平台 本项目基于Python编程环境,结合PyQt5图形界面开发库,设计实现了一套完整的图书信息管理解决方案。该系统主要面向图书馆、书店等机构的日常运营需求,通过模块化设计实现了图书信息的标准化管理流程。 系统架构采用典型的三层设计模式,包含数据存储层、业务逻辑层和用户界面层。数据持久化方案支持SQLite轻量级数据库与MySQL企业级数据库的双重配置选项,通过统一的数据库操作接口实现数据存取隔离。在数据建模方面,设计了包含图书基本信息、读者档案、借阅记录等核心数据实体,各实体间通过主外键约束建立关联关系。 核心功能模块包含六大子系统: 1. 图书编目管理:支持国际标准书号、中国图书馆分类法等专业元数据的规范化著录,提供批量导入与单条录入两种数据采集方式 2. 库存动态监控:实时追踪在架数量、借出状态、预约队列等流通指标,设置库存预警阈值自动提醒补货 3. 读者服务管理:建立完整的读者信用评价体系,记录借阅历史与违规行为,实施差异化借阅权限管理 4. 流通业务处理:涵盖借书登记、归还处理、续借申请、逾期计算等标准业务流程,支持射频识别技术设备集成 5. 统计报表生成:按日/月/年周期自动生成流通统计、热门图书排行、读者活跃度等多维度分析图表 6. 系统维护配置:提供用户权限分级管理、数据备份恢复、操作日志审计等管理功能 在技术实现层面,界面设计遵循Material Design设计规范,采用QSS样式表实现视觉定制化。通过信号槽机制实现前后端数据双向绑定,运用多线程处理技术保障界面响应流畅度。数据验证机制包含前端格式校验与后端业务规则双重保障,关键操作均设有二次确认流程。 该系统适用于中小型图书管理场景,通过可扩展的插件架构支持功能模块的灵活组合。开发过程中特别注重代码的可维护性,采用面向对象编程范式实现高内聚低耦合的组件设计,为后续功能迭代奠定技术基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值