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

查看:84
本文介绍了如何使用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.例如,您可以使用以下路径在document元素上获取默认的名称空间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(/*)

将为您提供该名称空间.

will give you that namespace.

另一种选择是忘记将前缀与该名称空间相关联,而只将路径设为无名称空间.每当需要引用不知道其命名空间的元素时,都可以使用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']

如果您确实想要的话,可以再进一步一步,针对document元素之一测试该元素的名称空间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天全站免登陆