Nokogiri:使用“:”搜索节点

Nokogiri: Searching node with ":"

我有一个简单的XML结构:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns:msg="..." xmlns:ld="...">
  <msg:testResultBatch providerId="12345" testName="Hello Labs">
    .
    .
    .
  </msg:testResultBatch>
</response>

当我将它传递给 Nokogiri.XML 时,例如:

req = Nokogiri.XML('
    <?xml version="1.0" encoding="utf-8"?>
    <response xmlns:msg="..." xmlns:ld="...">
      <msg:testResultBatch providerId="12345" testName="Hello Labs">
        .
        .
        .
      </msg:testResultBatch>
    </response>
')

我无法搜索带有“:”的节点。所以,

req.search("response") # works

但是,

req.search("msg:testResultBatch") # doesn't works

然后给我 []

通过使用 xpath 和 '//msg:testResultBatch' 你可以得到 msg:testResultBatch:

require 'nokogiri'

req = Nokogiri.XML('
<?xml version="1.0" encoding="utf-8"?>
<response xmlns:msg="..." xmlns:ld="...">
  <msg:testResultBatch providerId="12345" testName="Hello Labs">
  </msg:testResultBatch>
</response>
')
p req.xpath('//msg:testResultBatch').first.name # "testResultBatch"