Rspec/Rails/Nokogiri: Undefined namespace prefix

Viewed 23

Here is the output of node.to_xml

<rsm:ExchangedDocumentContext>
  <ram:GuidelineSpecifiedDocumentContextParameter>
    <ram:ID>urn:cen.eu:en16931:2017</ram:ID>
    <ram:GuidelineVersionID>2.1.1</ram:GuidelineVersionID>
  </ram:GuidelineSpecifiedDocumentContextParameter>
  <ram:BusinessProcessSpecifiedDocumentContextParameter>
    <ram:ID>urn:cen.eu:en16931:2017:poacc:billing:3.0</ram:ID>
    <ram:BusinessProcessVersionID>1.0</ram:BusinessProcessVersionID>
  </ram:BusinessProcessSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>

Here is the corresponding rspec test:

  describe '#to_node' do
    subject(:node) { exchanged_document_context.to_node }

    it 'returns the XML representation of the document' do
      expect(node).to be_a(Nokogiri::XML::Node)

      # Check the attributes of the ExchangedDocumentContext node
      expect(node.name).to eq('rsm:ExchangedDocumentContext')

      puts node.to_xml

      # Check the attributes of the GuidelineSpecifiedDocumentContextParameter node
      # rspec error is showing for the line below
      expect(node.at_xpath('ram:GuidelineSpecifiedDocumentContextParameter')).to be_a(Nokogiri::XML::Node)

      # Check the attributes of the BusinessProcessSpecifiedDocumentContextParameter node
      expect(node.at_xpath('ram:BusinessProcessSpecifiedDocumentContextParameter')).to be_a(Nokogiri::XML::Node)
    end
  end

But I'm getting this error:

 Nokogiri::XML::XPath::SyntaxError:
   ERROR: Undefined namespace prefix: ram:GuidelineSpecifiedDocumentContextParameter
1 Answers

because of the colon symbol (:), the XPath treated ram as a namespace instead of a node.

try this pattern instead:

...
nokogiri.at_xpath("//*[name()='ram:GuidelineSpecifiedDocumentContextParameter']")
Related