如何使用XPath忽略名称空间 [英] how to ignore namespaces with XPath

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

问题描述

我的目标是使用XPath从具有多个名称空间的多个xml文件中提取某些节点.只要我知道名称空间URI,一切都可以正常工作.名称空间名称本身保持不变,但是架构(XSD)有时是客户端生成的,即我不知道.然后基本上剩下三个选择:

My goal is to extract certain nodes from multiple xml files with multiple namespaces using XPath. Everything works fine as long as i know the namespace URIs. The namespace name itself remains constant, but the Schemas (XSD) are sometimes client-generated i.e. unknown to me. Then i am left with basically three choices :

  1. 仅对名称空间使用一种架构,希望没有任何问题(我确定吗?)

  1. use just one schema for the namespace, hoping nothing goes wrong (can i be sure?)

获取文档的子节点,并在第一个具有名称空间URI的节点上查找,希望在那里,然后仅使用URI,并在其上选择正确的URI.可能由于多种原因而出错

get the children nodes of the document and look for the first node with a namespace URI, hoping its there and just use the URI , hoping its the correct one. can go wrong for multiple reasons

以某种方式告诉xpath:看,我不在乎名称空间,只找到具有此名称的所有节点,我什至可以告诉您名称空间的名称,而不仅仅是URI".这是这里的问题...

somehow tell xpath : "look, i dont care about the namespaces, just find ALL nodes with this name, i can even tell you the name of the namespace, just not the URI". And this is the question here...

这不是对许多我的xpath表达式不起作用,因为我不了解名称空间意识"的重申,这里.我知道如何使用名称空间感知.只是不是如何摆脱它.

This is not a reiteration of numerous "my xpath expression doesnt work because i am not aware of namespace awareness" questions as found here or here. I know how to use namespace awareness. Just not how to get rid of it.

推荐答案

您可以使用local-name() XPath函数.而不是选择像这样的节点

You can use the local-name() XPath function. Instead of selecting a node like

/path/to/x:somenode

您可以选择所有节点,并使用正确的本地名称过滤该节点:

you can select all nodes and filter for the one with the correct local name:

/path/to/*[local-name() = 'somenode']

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

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