如何让Nokogiri理解我的命名空间?

Jam*_*sen 5 ruby xml xpath nokogiri

我有以下XML文档:

<samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217">
  <saml:NameID>@NOT_USED@</saml:NameID>
  <samlp:SessionIndex>abcdefg</samlp:SessionIndex>
</samlp:LogoutRequest>
Run Code Online (Sandbox Code Playgroud)

我想得到它的内容SessionIndex(即'abcdefg').我试过这个:

XPATH_QUERY = "LogoutRequest[@ID][@Version='2.0'][IssueInstant]/SessionIndex"
SAML_XMLNS  = 'urn:oasis:names:tc:SAML:2.0:assertion'
SAMLP_XMLNS = 'urn:oasis:names:tc:SAML:2.0:protocol'

require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.xpath(XPATH_QUERY, 'saml' => SAML_XMLNS, 'samlp' => SAMLP_XMLNS)
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Nokogiri::XML::SyntaxError: Namespace prefix samlp on LogoutRequest is not defined
Nokogiri::XML::SyntaxError: Namespace prefix saml on NameID is not defined
Nokogiri::XML::SyntaxError: Namespace prefix samlp on SessionIndex is not defined
Run Code Online (Sandbox Code Playgroud)

我已经尝试将命名空间添加到XPath查询中,但这并没有改变任何东西.

为什么我不能说服Nokogiri命名空间是有效的?

Jam*_*ett 9

我看到了两种不同的选择:

  1. 删除所有名称空间

    http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document#remove_namespaces%21-instance_method

    蛮力的做法.可能导致存在命名空间冲突的问题.

  2. 使用collect_namespaces

    http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document#collect_namespaces-instance_method

    一个更好的解决方案.你可以使用它一次来识别命名空间(例如在irb中)并对它们进行硬编码.

    要么

    在运行时使用它,并将其作为第二个参数提供给http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node#xpath-instance_method


Gre*_*ell 6

看起来这个文档中的命名空间没有被正确声明 - 根节点上应该有xmlns:samlpxmlns:saml属性.在这样的情况下,引入nokogiri基本上忽略了命名空间,如果你删除它们,也就是(因为它无法将它们映射到的URI或URN的),所以您的XPath的工作原理

doc.xpath(XPATH_QUERY)
Run Code Online (Sandbox Code Playgroud)