相关疑难解决方法(0)

如何让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命名空间是有效的?

ruby xml xpath nokogiri

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

nokogiri ×1

ruby ×1

xml ×1

xpath ×1