我有以下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命名空间是有效的?