加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:57:35 所属栏目:百科 来源:网络整理
导读:我有以下XML文档: samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217" saml:NameID@NOT_USED@/saml:NameID samlp:SessionIndexabcdefg/samlp:SessionIndex/samlp:LogoutRequest 我想从中获取SessionIndex(即’abcdefg’)的内容
我有以下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命名空间是有效的?

看起来这个文档中的命名空间没有被正确声明 – 根节点上应该有xmlns:samlp和xmlns:saml属性.在这种情况下,Nokogiri基本上忽略了命名空间(因为它无法将它们映射到URI或URN),因此如果删除它们,XPath就可以工作,即
doc.xpath(XPATH_QUERY)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读