سؤال

I have this working code:

require 'nokogiri'

NAMESPACES = {"xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:soap-enc" => "http://schemas.xmlsoap.org/soap/encoding/", "xmlns:cwmp" => "urn:dslforum-org:cwmp-1-0", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xml:xsd" => "http://www.w3.org/2001/XMLSchema"}

b = Nokogiri::XML::Builder.new
b[:soap].Envelope(NAMESPACES) {
    b[:soap].Header {}
    b[:soap].Body {
        b[:cwmp].GetParameterValues() {
            b.ParameterNames() {
                b.string "test"
            }
        }
    }
}

puts b.to_xml

which produces:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header/>
   <soap:Body>
      <cwmp:GetParameterValues>
         <cwmp:ParameterNames>
            <cwmp:string>test</cwmp:string>
         </cwmp:ParameterNames>
      </cwmp:GetParameterValues>
   </soap:Body>
</soap:Envelope>

Is there a way to avoid the namespace "cwmp" on "ParameterNames" and "string" nodes ?

Thanks

هل كانت مفيدة؟

المحلول

One way is to add a default namespace to the list of namespaces. Nokogiri will use the default namespace instead of the namespace from the parent-node.

Just add the default namespace to your NAMESPACES-hash. The default pattern for the default namespace is "xmlns" => "http://someuri.org/"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top