XML 中的命名空间使用 [英] Namespace usage in XML

查看:32
本文介绍了XML 中的命名空间使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pugi xml 解析 XML,它不是命名空间感知的解析器(参见 使用 Boost 读写 XML 文件).我试图弄清楚这可能有多大的问题,但问题是我不是 100% 了解 XML 命名空间的用途.

I'm parsing XML using pugi xml, which is not a namespace-aware parser (see Using Boost to read and write XML files). I'm trying to figure out how much of an issue this might be, but the problem is I don't 100% understand what XML namespaces are used for.

以下是一些有问题的 XML(我创建的)示例:

Here is an example of some XML (that I created) that would be problematic:

<Results>
    <Documents xmlns:active="...">
        <Document>...</Document>
    </Documents>
    <Documents xmlns:archived="...">
        <Document>...</Document>
        <Document>...</Document>
        <Document>...</Document>
        <Document>...</Document>
    </Documents>
</Results>

给定一个像 /Results/Documents/Document 这样的 XPath 表达式,pugi 能为我做的就是提取所有 元素——我会丢失活动/存档信息.但是,我不确定在现实世界中是否遇到过这种类型的命名空间使用.在这种情况下,似乎最好使用属性来获取活动/存档信息.有人可以帮助我更好地了解使用命名空间的情况,以便我更好地了解坚持使用 pugi xml 会失去什么吗?

Given an XPath expression like /Results/Documents/Document, all pugi could do for me is extract all <Document> elements -- I'd lose the active/archived information. However, I'm not sure if I'd ever encounter this type of namespace use in the real world. It seems like in this situation it would be better use an attribute to get across the active/archived information. Can someone help me better understand the situations namespaces are used in so I can get a better idea for what I'd be losing out on by sticking with pugi xml?

推荐答案

虽然默认命名空间声明(例如 xmlns="http://www.example.com")和命名空间前缀声明(例如xmlns:e="http://www.example.com") 类似于属性,XML 命名空间的用途与 XML 属性完全不同.

Although default namespace declarations (e.g. xmlns="http://www.example.com") and namespace prefix declarations (e.g. xmlns:e="http://www.example.com") resemble attributes, XML namespaces serve a completely different purpose than XML attributes.

XML 命名空间的目的是允许将独立开发的 XML 词汇表组合到单个 XML 文档中,同时提供一种将名称与词汇表相关联的方法以及避免词汇表之间命名冲突的方法.

XML 属性的目的是表示数据或元数据.(请参阅XML 属性与 XML 元素.)

The purpose of XML attributes is to represent data or metadata. (See XML attribute vs XML element.)

您是否可以接受任何给定的 XML 或 XPath 处理器对 XML 名称空间的支持程度将取决于支持(不)支持的内容的详细信息以及您的需求的详细信息.但是请注意,不合规有一个严重的缺点:您已经已经目睹了合规和不合规处理器之间惊人的分歧,其中相同的 XPath 为相同的 XML 文档返回完全不同的结果.互操作性优先于合规性.

Whether the degree of support for XML namespaces by any given XML or XPath processor is acceptable to you will depend upon the both the particulars of what's (not) supported as well as the particulars of your needs. Do note, however, that noncompliance has a serious disadvantage: You've already witnessed surprising disagreement between compliant and noncompliant processors where the same XPath returned completely different results for the same XML document. Interoperability prefers compliance.

这篇关于XML 中的命名空间使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆