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

查看:45
本文介绍了如何使用 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. 仅对命名空间使用一种架构,希望不会出错(我可以确定吗?).
  2. 获取文档的子节点并查找具有命名空间 URI 的第一个节点,希望它在那里,然后使用 URI,希望它是正确的.由于多种原因,这可能会出错
  3. 以某种方式告诉 xpath :看,我不在乎名称空间,只要找到具有此名称的所有节点,我什至可以告诉您名称空间的名称,而不是 URI".这就是这里的问题......

这不是重复许多我的 xpath 表达式不起作用,因为我不知道命名空间意识";在此处此处.我知道如何使用命名空间感知,只是不知道如何摆脱它.

This is not a reiteration of numerous "my xpath expression doesn't 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天全站免登陆