[libxml-devel] Creating a node with multiple name spaces

Isak Hansen isak.hansen at gmail.com
Tue Apr 1 06:15:06 EDT 2008


I'm having trouble defining multiple name spaces for a node, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
         xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
	 xmlns:udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2">
    [...]
</Invoice>


I have the following code, which adds a default name space, but can't
figure out from the api docs how to create additional prefixed name
spaces:

require 'rubygems'
require 'xml/libxml'

doc = XML::Document.new
doc.root = XML::Node.new('Invoice')
root = doc.root
root.namespace = 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'

# TODO; add prefixed name spaces to root:
# xmlns:udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2"
# xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"

# print document
puts doc.to_s


I could parse a dummy xml document and use that as a skeleton, but
would like to build it using a dom-ish approach if possible. Just
adding "xmlns:udt" and "xmlns:qdt" as attributes to root doesn't seem
to do the proper thing. Someone able to lend a hand?


Regards,
Isak


More information about the libxml-devel mailing list