Charlie Savage skrev:
I am experiencing some problems when constructing a document piece by piece. I have boiled downed the problem to the following code:

require 'rubygems'
require 'xml/libxml'

# Envelope XML
xml_string = %{<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</soapenv:Envelope>}

# Create Document
parser = XML::Parser.string(xml_string, :options => XML::Parser::Options::NOBLANKS)

What happens if you first call XML.default_keep_blanks = false?  Should do the same thing as the parser option, but worth a quick double check.

And also, take a look at this message from the libxml creator.  He strongly discourages the use of NOBLANKS.

http://mail.gnome.org/archives/xml/2007-November/msg00022.html

Charlie

Hi

Thanks for the response and suggestions. I tried adding the line you suggest which results in the following:

require 'rubygems'
require 'xml/libxml'

# Envelope XML
xml_string = %{<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</soapenv:Envelope>}

XML.default_keep_blanks = false

# Create Document
parser = XML::Parser.string(xml_string)
doc = parser.parse

# Add body
envelope_node = doc.find_first("/ns:Envelope", "ns:http://schemas.xmlsoap.org/soap/envelope/")
body_node = XML::Node.new("soapenv:Body")
envelope_node << body_node

puts envelope_node.children.size # => 2, the extre node being a node with content "\n"

# Remove white space in XML string
xml_string.gsub!(/[\n]/, "")

# Create Document with clean string
parser = XML::Parser.string(xml_string)
doc = parser.parse

# Add body
envelope_node = doc.find_first("/ns:Envelope", "ns:http://schemas.xmlsoap.org/soap/envelope/")
body_node = XML::Node.new("soapenv:Body")
envelope_node << body_node

puts envelope_node.children.size # => 1, the correct Body node

This yields the same results as before, though.

Regarding the use of NOBLANKS, I am actually glad it is not required (and even discouraged). I nly tried it once the initial code did not function as I expected. I cannot think of a scenario where you do want a single "\n" to become a separate node, so I think it should be default behaviour behaviour to not make it so.

Do you have any other suggestions what might be wrong?

/Erik


_______________________________________________ libxml-devel mailing list libxml-devel@rubyforge.org http://rubyforge.org/mailman/listinfo/libxml-devel



_______________________________________________
libxml-devel mailing list
libxml-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/libxml-devel

Reply via email to