in reply to Creating Nodes in namespace with XML::LibXML
Please have a look at How do I post a question effectively? - you haven't given a runnable piece of code, the expected output, and the current output.
You seem to want to avoid passing the document object around like it's a bad thing - it's not, in fact, if I understand your question correctly, then the solution is based on that you will need to pass the XML::LibXML::Document object around and add your elements to the document as you create them, instead of handling unbound nodes.
use warnings; use strict; use XML::LibXML; my $doc = XML::LibXML::Document->new; my $el1 = $doc->createElementNS("namespace0","aaa"); $el1->setNamespace("namespace1","p1",0); $el1->setNamespace("namespace2","p2",0); $doc->setDocumentElement($el1); my $el2 = $doc->createElement("bbb"); $el2->setNamespace("namespace2","p2",1); $el1->appendChild($el2); my $el3 = $doc->createElementNS("namespace0","ccc"); $el1->appendChild($el3); my $el4 = $doc->createElement("p1:ddd"); $el3->appendChild($el4); print $doc->toString(1); __END__ <?xml version="1.0"?> <aaa xmlns="namespace0" xmlns:p1="namespace1" xmlns:p2="namespace2"> <p2:bbb/> <ccc> <p1:ddd/> </ccc> </aaa>
Note how the namespace declarations are automatically reused. This is actually documented in XML::LibXML::Element's setNamespace. Since you seem to want to work with XML::LibXML more closely, I strongly suggest you take the time to read all of its documentation (starting with XML::LibXML::Node, XML::LibXML::Element and XML::LibXML::Document).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating Nodes in namespace with XML::LibXML
by worik (Sexton) on Jun 05, 2015 at 01:42 UTC |