Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nokogiri::XML::Builder doesn't support "-" (hyphens) in namespace prefixes #531

Closed
parameme opened this issue Sep 6, 2011 · 0 comments
Closed

Comments

@parameme
Copy link

parameme commented Sep 6, 2011

While trying to get a .Net bug-compatible SOAP output I found the following anomaly...

require 'nokogiri'

SOAP_NAMESPACES =  {
                        "xmlns:SOAP-ENV"=>"http://schemas.xmlsoap.org/soap/envelope/",
                        "xmlns:SOAP-ENC"=>"http://www.w3.org/2003/05/soap-encoding",
                        "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
                        "xmlns:xsd"=>"http://www.w3.org/2001/XMLSchema",
                        "xmlns:wsa"=>"http://schemas.xmlsoap.org/ws/2004/08/addressing",
                        "xmlns:custom"=>"http://local.dev/soap_api/v1"
                   }


# Namespaces explicitly post constructed
builder = Nokogiri::XML::Builder.new( :encoding => 'UTF-8' ) do |xml|
  xml.Envelope do
    #Workaround to allow namespace prefixes with "-" (hyphens) in them
    SOAP_NAMESPACES.each do | prefix, href |
      xml.doc.root.add_namespace_definition( prefix.gsub( 'xmlns:', '' ), href )
    end
    #Workaround to allow SOAP envelope to be namespaced after namespace defined
    xml.doc.root.namespace = xml.doc.root.namespace_definitions.first
  end
end

puts "Expected Output :"
puts builder.doc.to_xml

# Namespaces inline with root element
builder = Nokogiri::XML::Builder.new( :encoding => 'UTF-8' ) do |xml|
  xml.Envelope(
                SOAP_NAMESPACES
              ) do
    #Workaround to allow SOAP envelope to be namespaced after namespace defined
    xml.doc.root.namespace = xml.doc.root.namespace_definitions.first
  end
end

puts "Actual Output :"
puts builder.doc.to_xml

Expected Output :

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:custom="http://local.dev/soap_api/v1"/>

Actual Output :

<?xml version="1.0" encoding="UTF-8"?>
<xsi:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:custom="http://local.dev/soap_api/v1" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"/>

If reasonable I think it should be a simple regex change in nokogiri-1.5.0/lib/nokogiri/xml/document.rb (around line : 60)

            arg.each { |k,v|
              key = k.to_s
              if key =~ /^xmlns(:\w+)?$/  #Change "\w+" to "[0-9A-Za-z_-]+" or whatever is XML NCName compliant
                ns_name = key.split(":", 2)[1]
                elm.add_namespace_definition ns_name, v
                next
              end
              elm[k.to_s] = v.to_s
            }

Regards,
parameme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants