worik has asked for the wisdom of the Perl Monks concerning the following question:
I wish to build an XML document up node by node. The nodes will be build in functions that call functions to build child nodes and append them. Quite a straight forward way of doing XML, IMO.
All the methods I have seen to build a node that is in a namespace http://www.xml.com/pub/a/2001/11/14/xml-libxml.html?page=2 for instance need the XML::LibXML::Document. To achieve this I would need to pass a XML::LibXML::Document element around the functions or have it as a global.
What I really want is a version of XML::LibXML::Element->new(<name>, <name space>)
XML::LibXML::Element->setNamespace is not what I want as it adds namespase declarations
An example of what I am trying to do...
# Create (local) root node named $rname, the children will be $cname1 + and $cname2, $cname1 is in the same namespace as $rname and $cname2 +is in a different namespace my $node = XML::LibXML::Element->new($rname); # I want to declare the namespaces and prefixes the children will use. + NS_name1 is the namespace of the node itself but it has child nodes +that use NS_name2 $node->setNamespace(NS_name1, NS_prefix1, 1); $node->setNamespace(NS_name2, NS_prefix2, 0); # Creating the children. Thi sis not valid code, this is what I want my $cnode1 = XML::LibXML::Element->new($cname1, $NS_name1); my $cnode2 = XML::LibXML::Element->new($cname2, $NS_name2); $node->appendChild($cnode1); $node->appendChild($cnode2); return $node;
This way I need global knowledge of the name space names (and prefixes, but it would be trivial to avoid that) but I do not need to pass the complete document around as an argument or make it global
Is there some way of doing this (a two argument version of XML::LibXML::Element::new) or do I need to have the root of the tree accessible to the code making leaves/sub-trees?
Worik
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating Nodes in namespace with XML::LibXML
by Anonymous Monk on Jun 04, 2015 at 23:49 UTC | |
by worik (Sexton) on Jun 05, 2015 at 01:42 UTC | |
|
Re: Creating Nodes in namespace with XML::LibXML
by choroba (Cardinal) on Jun 05, 2015 at 10:39 UTC |