【发布时间】:2012-01-06 16:25:59
【问题描述】:
我的问题与“How can I get the values of attributes with namespace, using Nokogiri?”不同。
我的 XML 包含带有命名空间的属性。如何使用 Nokogiri 的 css 方法查询包含 attributeNameA 和 namespaceB (namespaceB:attributeNameA="attributeAValue") 的元素?
这是我的代码:
xmlContent = %Q|
<?xml version="1.0"?>
<ns1:el1 xmlns:ns1="blabla" >
<ns1:el2 ns1:att="123">with namespace</ns1:el2 >
<ns1:el2 att="noNameSpace">no namespace</ns1:el2 >
</ns1:el1>|
xml_doc = Nokogiri::XML(xmlContent)
#no namespace
result = xml_doc.css('ns1|el2[att]')
result.each {|i| puts i}
#with namespace
result = xml_doc.css('ns1|el2[ns1|att]') #error unexpexted '|'
result.each {|i| puts i}
2011 年 1 月 6 日编辑: 从这个链接: https://github.com/tenderlove/nokogiri/issues/257#issuecomment-3365636Nokogiri 不支持直接查询带有命名空间属性的xml元素。
真正的用例是修改 SSIS 包 (*.dtsx);我无法查询属性 DTS:Name 包含“projectVar_”的所有元素。
我必须将此用例通知 nokogiri 团队。
【问题讨论】:
标签: ruby xml-parsing css-selectors nokogiri