如何使用 Xpath 检索 XML 文件中的命名空间 [英] How to retrieve namespaces in XML files using Xpath

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

问题描述

我有一个这样开头的 XML 文件:

I have an XML file that starts like this:

<Elements name="Entities" xmlns="XS-GenerationToolElements">

我必须打开很多这样的文件.它们中的每一个都有不同的命名空间,但一次只能有一个命名空间(我永远不会在一个 xml 文件中找到两个命名空间).

I'll have to open a lot of these files. Each of these have a different namespace but will only have one namespace at a time (I'll never find two namespaces defined in one xml file).

使用 XPath 我希望有一种自动方式将给定的命名空间添加到命名空间管理器.到目前为止,我只能通过解析 xml 文件来获取命名空间,但我有一个 XPathNavigator 实例,它应该有一种很好且干净的方式来获取命名空间,对吧?

Using XPath I'd like to have an automatic way to add the given namespace to the namespace manager. So far, i could only get the namespace by parsing the xml file but I have a XPathNavigator instance and it should have a nice and clean way to get the namespaces, right?

-- 或者--

鉴于我只有一个命名空间,以某种方式让 XPath 使用 xml 中唯一存在的命名空间,从而通过始终附加命名空间来避免代码混乱.

Given that I only have one namespace, somehow make XPath use the only one that is present in the xml, thus avoiding cluttering the code by always appending the namespace.

推荐答案

您可以尝试一些技巧;您使用哪一个将取决于您需要从文档中获取哪些信息、您想要的严格程度以及您使用的 XPath 实现的一致性程度.

There are a few techniques that you might try; which you use will depend on exactly what information you need to get out of the document, how rigorous you want to be, and how conformant the XPath implementation you're using is.

获取与特定前缀关联的命名空间 URI 的一种方法是使用 namespace:: 轴.这将为您提供一个名称空间节点,其名称是前缀,其值是名称空间 URI.例如,您可以使用以下路径获取文档元素上的默认命名空间 URI:

One way to get the namespace URI associated with a particular prefix is using the namespace:: axis. This will give you a namespace node whose name is the prefix and whose value is the namespace URI. For example, you could get the default namespace URI on the document element using the path:

/*/namespace::*[name()='']

您也许可以使用它为您的 XPathNavigator 设置名称空间关联.但请注意,namespace:: 轴是 XPath 1.0 中并不总是实现的那些角落之一.

You might be able to use that to set up the namespace associations for your XPathNavigator. Be warned, though, that the namespace:: axis is one of those corners of XPath 1.0 that isn't always implemented.

获取该命名空间 URI 的第二种方法是在文档元素上使用 namespace-uri() 函数(您说过将始终在该命名空间中).表达式:

A second way of getting that namespace URI is to use the namespace-uri() function on the document element (which you've said will always be in that namespace). The expression:

namespace-uri(/*)

会给你那个命名空间.

另一种方法是忘记将前缀与该命名空间相关联,而只是让您的路径无命名空间.当您需要引用一个您不知道其名称空间的元素时,您可以使用 local-name() 函数来做到这一点.例如:

An alternative would be to forget about associating a prefix with that namespace, and just make your path namespace-free. You can do this by using the local-name() function whenever you need to refer to an element whose namespace you don't know. For example:

//*[local-name() = 'Element']

如果你真的想要,你可以更进一步,根据文档元素之一测试元素的命名空间 URI:

You could go one step further and test the namespace URI of the element against the one of the document element, if you really wanted:

//*[local-name() = 'Element' and namespace-uri() = namespace-uri(/*)]

考虑到命名空间对您来说似乎没有任何意义,最后一个选择是通过一个过滤器运行您的 XML,该过滤器去除命名空间.然后,您根本不必担心 XPath 中的它们.最简单的方法是使用正则表达式删除 xmlns 属性,但如果您需要同时进行其他整理,您可以做一些更复杂的事情.

A final option, given that the namespace seems to mean nothing to you, would be to run your XML through a filter that strips out the namespaces. Then you won't have to worry about them in your XPath at all. The easiest way to do that would be simply to remove the xmlns attribute with a regular expression, but you could do something more complex if you needed to do other tidying at the same time.

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

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