Xml 如何让Nokogiri理解我的名称空间?
我有以下XML文档:Xml 如何让Nokogiri理解我的名称空间?,xml,ruby,xpath,nokogiri,Xml,Ruby,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> 但我得到以下错误:
<samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217">
<saml:NameID>@NOT_USED@</saml:NameID>
<samlp:SessionIndex>abcdefg</samlp:SessionIndex>
</samlp:LogoutRequest>
但我得到以下错误:
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)
我为您提供了两种不同的选择:
这似乎给了我同样的错误。。。在某些情况下。在irb中做这件事很好,但运行我的规范仍然会失败。盖尔,明白了!Nokogiri返回一个“错误”节点,该节点包装了它找到的节点,但它确实找到了该节点!
doc.xpath(XPATH_QUERY)