Tuesday, 17 February 2015

Ruby/Sinatra Parse XML using Nokogiri but unable to resolve namespaces



This is my first post and hope someone can help me. The vendor sent me a XML message and my script need to be able to parse the "AccountStatus" node element. My script works correctly if the XML posted without namespaces. WHen I included the original Namespaces, a large chunk of XML data will be missing when the script executes Nokogiri::XML(request.body.read).


Here is the XML sample:





curl -i -H -X POST -d "<?xml version="1.0" encoding="utf-8" ?>
<DocuSignEnvelopeInformation xmlns="http://ift.tt/tphNwY">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
<CustomFields />
<AccountStatus>Active</AccountStatus>
<RecipientId>ab2bf57b-72b7-48e7-8298-b1c7b56930b9</RecipientId>
</RecipientStatus>
</RecipientStatuses>
</EnvelopeStatus>
</DocuSignEnvelopeInformation>" localhost:4567/shunt?uri=http://ift.tt/1EIBRR4



Here is my scripts:





require 'rubygems'
require 'sinatra'
require 'uri'
require 'nokogiri'
#require 'pry'
#require 'pp'

post '/shunt' do
puts "hello world"

xmldoc = Nokogiri::XML(request.body.read)
puts xmldoc

xmldoc.xpath("//docu:DocuSignEnvelopeInformation/EnvelopeStatus/RecipientStatuses/RecipientStatus", "docu"=>"http://ift.tt/tphNwY").each do |node|
puts node.text
end
end



Here is the output:





hello world
<?xml version="1.0"?>
<DocuSignEnvelopeInformation/>



No comments:

Post a Comment