How do I get Nokogiri to understand my namespaces?

后端 未结 2 1357
迷失自我
迷失自我 2020-12-17 02:23

I have the following XML document:


  @NOT_USED@         


        
相关标签:
2条回答
  • 2020-12-17 02:55

    I see a two different options for you:

    1. Remove all the namespaces

      http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document#remove_namespaces%21-instance_method

      Brute force way of doing it. Could lead to problems where there are namespace collisions.

    2. Use collect_namespaces

      http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document#collect_namespaces-instance_method

      A much better solution. You could use this once to identify the namespaces (say in irb) and hard-code them.

      OR

      Use it at runtime, and supply it as the second argument to https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Searchable#xpath-instance_method

    0 讨论(0)
  • 2020-12-17 03:19

    It doesn't look like the namespaces in this document are correctly declared - there should be xmlns:samlp and xmlns:saml attributes on the root node. In cases like this, Nokogiri essentially ignores the namespaces (as it can't map them to URIs or URNs), so your XPath works if you remove them, i.e.

    doc.xpath(XPATH_QUERY)
    
    0 讨论(0)
提交回复
热议问题