XPath 如何处理 XML 命名空间?
如果我用
/IntuitResponse/QueryResponse/Bill/Id
为了解析下面的 XML 文档,我返回了 0 个节点。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3"
time="2016-10-14T10:48:39.109-07:00">
<QueryResponse startPosition="1" maxResults="79" totalCount="79">
<Bill domain="QBO" sparse="false">
<Id>=1</Id>
</Bill>
</QueryResponse>
</IntuitResponse>
但是,我没有在 XPath 中指定命名空间(即 http://schema.intuit.com/finance/v3 不是路径的每个标记的前缀)。如果我不明确告诉它,XPath 如何知道我想要哪个 Id?我想在这种情况下(因为只有一个命名空间)XPath 可以完全忽略 xmlns 。但如果有多个命名空间,事情就会变得很糟糕。
最佳答案
XPath 本身没有办法将命名空间前缀与命名空间绑定(bind)。此类设施由托管图书馆提供。
建议您使用这些工具并定义命名空间前缀,然后可以根据需要使用这些前缀来限定 XML 元素和属性名称。
下面是 XPath 主机提供的一些不同机制,用于指定命名空间前缀绑定(bind)到命名空间 URI。
(OP 的原始 XPath,/IntuitResponse/QueryResponse/Bill/Id,已被省略为 /IntuitResponse/QueryResponse。)
C#:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("i", "http://schema.intuit.com/finance/v3");
XmlNodeList nodes = el.SelectNodes(@"/i:IntuitResponse/i:QueryResponse", nsmgr);
Google 文档:
不幸的是,IMPORTXML()不提供 namespace 前缀绑定(bind)机制。请参阅下一节击败 XPath 中的 namespace ,了解如何使用 local-name() 作为解决方法。
Java (SAX):
NamespaceSupport support = new NamespaceSupport();
support.pushContext();
support.declarePrefix("i", "http://schema.intuit.com/finance/v3");
Java(XPath):
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
switch (prefix) {
case "i": return "http://schema.intuit.com/finance/v3";
// ...
}
});
DocumentBuilderFactory.setNamespaceAware(true) .JavaScript:
参见 Implementing a User Defined Namespace Resolver :
function nsResolver(prefix) {
var ns = {
'i' : 'http://schema.intuit.com/finance/v3'
};
return ns[prefix] || null;
}
document.evaluate( '/i:IntuitResponse/i:QueryResponse',
document, nsResolver, XPathResult.ANY_TYPE,
null );
请注意,如果默认命名空间定义了关联的命名空间前缀,则使用 Document.createNSResolver() 返回的 nsResolver()可以消除对客户 nsResolver() 的需求。
Perl ( LibXML ):
my $xc = XML::LibXML::XPathContext->new($doc);
$xc->registerNs('i', 'http://schema.intuit.com/finance/v3');
my @nodes = $xc->findnodes('/i:IntuitResponse/i:QueryResponse');
Python(lxml):
from lxml import etree
f = StringIO('<IntuitResponse>...</IntuitResponse>')
doc = etree.parse(f)
r = doc.xpath('/i:IntuitResponse/i:QueryResponse',
namespaces={'i':'http://schema.intuit.com/finance/v3'})
Python(ElementTree):
namespaces = {'i': 'http://schema.intuit.com/finance/v3'}
root.findall('/i:IntuitResponse/i:QueryResponse', namespaces)
Python(Scrapy):
response.selector.register_namespace('i', 'http://schema.intuit.com/finance/v3')
response.xpath('/i:IntuitResponse/i:QueryResponse').getall()
PhP:
改编自@Tomalak's answer using DOMDocument :
$result = new DOMDocument();
$result->loadXML($xml);
$xpath = new DOMXpath($result);
$xpath->registerNamespace("i", "http://schema.intuit.com/finance/v3");
$result = $xpath->query("/i:IntuitResponse/i:QueryResponse");
另见 @IMSoP's canonical Q/A on PHP SimpleXML namespaces .
ruby (Nokogiri):
puts doc.xpath('/i:IntuitResponse/i:QueryResponse',
'i' => "http://schema.intuit.com/finance/v3")
请注意,Nokogiri 支持删除命名空间,
doc.remove_namespaces!
但是看到下面的警告阻止了 XML namespace 的失败。
VBA:
xmlNS = "xmlns:i='http://schema.intuit.com/finance/v3'"
doc.setProperty "SelectionNamespaces", xmlNS
Set queryResponseElement =doc.SelectSingleNode("/i:IntuitResponse/i:QueryResponse")
VB.NET:
xmlDoc = New XmlDocument()
xmlDoc.Load("file.xml")
nsmgr = New XmlNamespaceManager(New XmlNameTable())
nsmgr.AddNamespace("i", "http://schema.intuit.com/finance/v3");
nodes = xmlDoc.DocumentElement.SelectNodes("/i:IntuitResponse/i:QueryResponse",
nsmgr)
SoapUI(doc):
declare namespace i='http://schema.intuit.com/finance/v3';
/i:IntuitResponse/i:QueryResponse
xmlstarlet:
-N i="http://schema.intuit.com/finance/v3"
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://schema.intuit.com/finance/v3">
...
一旦声明了 namespace 前缀,就可以编写 XPath 来使用它:
/i:IntuitResponse/i:QueryResponse
另一种方法是编写针对 local-name() 进行测试的谓词:
/*[local-name()='IntuitResponse']/*[local-name()='QueryResponse']
或者,在 XPath 2.0 中:
/*:IntuitResponse/*:QueryResponse
以这种方式绕过命名空间可行,但不推荐,因为它
未指定完整的元素/属性名称。
无法区分不同语言中的元素/属性名称 命名空间(命名空间的目的)。请注意,可以通过添加额外的谓词来显式检查 namespace URI 来解决此问题:
/*[ namespace-uri()='http://schema.intuit.com/finance/v3'
and local-name()='IntuitResponse']
/*[ namespace-uri()='http://schema.intuit.com/finance/v3'
and local-name()='QueryResponse']
感谢 Daniel Haley对于 namespace-uri() 注释。
过于冗长。
支持现代 XPath 3.0/3.1 的库和工具允许直接在 XPath 表达式中指定命名空间 URI:
/Q{http://schema.intuit.com/finance/v3}IntuitResponse/Q{http://schema.intuit.com/finance/v3}QueryResponse
虽然 Q{http://schema.intuit.com/finance/v3} 比使用 XML 命名空间前缀要冗长得多,但它具有独立于命名空间前缀绑定(bind)的优点托管库的机制。 Q{} 表示法以其创始人 James Clark 的名字命名为 Clark Notation。 W3C XPath 3.1 EBNF 语法将其称为 BracedURILiteral .
感谢 Michael Kay对于覆盖 XPath 3.0/3.1 的 BracedURILiteral 的建议。
关于xml - XPath 如何处理 XML namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796231/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
遍历nokogiri并使用xpath选择元素相当容易。我需要这个反之亦然,意思是:我需要例如在nokogiri节点上调用.to_xpath以获取元素的完整xpath以将其存储在记录中。有人知道这样做的方法吗? 最佳答案 我能想到的最简单的方法是:Nokogiri::CSS.xpath_fornode.css_path编辑:您也可以尝试使用path方法。 关于ruby-如何从nokogiri对象获取标识符(例如xpath)?,我们在StackOverflow上找到一个类似的问题:
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我设置了以下模型:classContact:no_freq?validates_presence_of:freq,:if=>:no_band?protecteddefno_freq?freq.nil?enddefno_band?band.nil?endendclassBand当我在我的新View中输入频率时,如果输入了频率,则不允许指定波段。这在我的其他观点中造成了问题,因为band现在为零。我如何允许不指定band并在我的index和showView中显示为空,然后在editView中允许在以后指定一个。通过执行以下操作,我已经能够让我的索引显示空白:contact.band&&co