【问题标题】:How do I get Nokogiri to understand my namespaces?如何让 Nokogiri 了解我的命名空间?
【发布时间】:2010-10-24 18:08:46
【问题描述】:

我有以下 XML 文档:

<samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217">
  <saml:NameID>@NOT_USED@</saml:NameID>
  <samlp:SessionIndex>abcdefg</samlp:SessionIndex>
</samlp:LogoutRequest>

我想从中获取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)

但我收到以下错误:

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

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

为什么我不能让 Nokogiri 相信命名空间是有效的?

【问题讨论】:

    标签: xml ruby xpath nokogiri


    【解决方案1】:

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

    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 中)并对其进行硬编码。

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

    【讨论】:

      【解决方案2】:

      本文档中的命名空间似乎没有正确声明 - 根节点上应该有 xmlns:samlpxmlns:saml 属性。在这种情况下,Nokogiri 基本上会忽略名称空间(因为它无法将它们映射到 URI 或 URN),因此如果您删除它们,您的 XPath 就可以工作,即

      doc.xpath(XPATH_QUERY)
      

      【讨论】:

      • 这似乎给了我同样的错误......在某些情况下。在 irb 中按字面意思进行操作可以正常工作,但运行我的规范仍然会失败。加尔。
      • 知道了! Nokogiri 返回一个“错误”节点,它包装了它找到的节点,但它really_did_find_the_node
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2015-04-11
      相关资源
      最近更新 更多