Petr Pajas, maintainer of XML::LibXML, responded and gave permission to
forward his reply to the list.
-------- Original Message --------
Subject: Re: XML::LibXML::Document and namespaces
Date: Tue, 17 Nov 2009 22:58:41 +0100
From: Petr Pajas <pa...@...>
Hi Tom,
2009/11/17 Tom Metro:
> I believe I could have also replaced:
>
> my $id = $root->addNewChild($nsURI, "$pfx:Id");
>
> with:
>
> my $id = XML::LibXML::Element->new("Id");
> $id->setNamespace($nsURI , $pfx);
> $root->addChild($id);
XML::LibXML implements parts of the DOM Level 2 and Level 3 spec (and
provides some extensions to it), see
http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1590626202
The DOM way of doing this is
my $id = $doc->createElementNS($nsURI,"$pfx:Id");
$root->appendChild($id);
> which seems more correct, but isn't much of a win from a conciseness
> perspective, though it could be consolidated into a helper method.
>
> Am I missing something in the DOM API, or is it really this cumbersome
> to deal with namespaces?
Yes, I'm affraid it is. Of course, you can use the default namespace
instead of a prefix and so get rid of the $pfx: stuff, but DOM API
needs you to pass the $nsURI to every method where you create a new
node.
Also, instead of creating the DOM tree from scratch, for documents
with a rigid structure, you could also proceed by parsing a template
XML string and then filling in the few variable values (which may also
perform much better).
P.S. I'm Cc-ing the list, but I'm not a subscriber; you may need to
forward my answer if it doesn't get there from me.
-- Petr
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm