Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove the namespace from few XML nodes
    text
    copied!<p>I need to read an XML file and generate a SOAP request body using Nokogiri in Ruby on Rails.</p> <p>The request body that I need to generate is:</p> <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c2="http://c2_0.customer.webservices.csx.dtv.com/"&gt; &lt;soapenv:Header&gt; &lt;soapenv:Body&gt; &lt;c2:getCustomer&gt; &lt;customerId&gt;10&lt;/customerId&gt; &lt;UserId&gt;adminUser&lt;/UserId&gt; &lt;/c2:getCustomer&gt; &lt;/soapenv:Body&gt; &lt;/soapenv:Header&gt; &lt;/soapenv:Envelope&gt; </code></pre> <p>I am using this code:</p> <pre><code>require 'nokogiri' doc = Nokogiri::XML(File.open('p_l_s.xml')) wsID =doc.xpath('//transaction:WsID' , 'transaction' =&gt; 'http://www.nrf-arts.org/IXRetail/namespace/').inner_text builder = Nokogiri::XML::Builder.new do |xml| xml.Envelope("xmlns:soapenv" =&gt; "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:c2" =&gt; "http://c2_0.customer.webservices.csx.dtv.com/") do xml.parent.namespace = xml.parent.namespace_definitions.first xml['soapenv'].Header { xml.Body { xml['c2'].getCustomer{ #xml.remove_namespaces! xml.customerId wsID xml.UserId "adminUser" } } } end end puts builder.to_xml </code></pre> <p>And, when executing it from the terminal in Ubuntu, I get:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c2="http://c2_0.customer.webservices.csx.dtv.com/"&gt; &lt;soapenv:Header&gt; &lt;soapenv:Body&gt; &lt;c2:getCustomer&gt; &lt;c2:customerId&gt;10&lt;/c2:customerId&gt; &lt;c2:UserId&gt;adminUser&lt;/c2:UserId&gt; &lt;/c2:getCustomer&gt; &lt;/soapenv:Body&gt; &lt;/soapenv:Header&gt; &lt;/soapenv:Envelope&gt; </code></pre> <p>I get the <code>c2</code> namespace for the XML elements <code>customerId</code> and <code>UserId</code> which is not required for the method I'm invoking in the WSDL file I will be calling.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload