[libxml-devel] Strange node being created from white space

Erik Lindblad erik at aldm.se
Sun Feb 1 17:12:39 EST 2009


Hi

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)
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, :options => 
XML::Parser::Options::NOBLANKS)
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

The code first creates an XML::Document from an XML string. It then adds 
a Body node to the root Envelope node. The problem is that in the first 
example (where the line breaks are kept) results in an extra node being 
created. I provided the NOBLANKS option to show that this does not solve 
the problem, but the extra node is created whether it is provided or not.

Does anyone have some input on this. I am running the latest 0.9.8 
release (great job with it btw).


Kindest regards

Erik



More information about the libxml-devel mailing list