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).


In reply to Re: Creating Nodes in namespace with XML::LibXML by Anonymous Monk
in thread Creating Nodes in namespace with XML::LibXML by worik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.