you haven't given a runnable piece of code
True. My bad. Sorry

That said I have worked out the XML::LibXML approach.

For example I want to build:

<?xml version="1.0" encoding="UTF-8"?> <O:ABC xmlns:O="ONE:" xmlns:T="two:IS:a_namEsp"> <O:ZZZ/> <T:ZZZ> <O:QWERTY> <T:UIOP/> </O:QWERTY> </T:ZZZ> </O:ABC>

The following code does it....

#!/usr/bin/perl -w use strict; use XML::LibXML; # NAmespaces my $ONE_ns = 'ONE:'; my $ONE_pfx = 'O'; my $TWO_ns = 'two:IS:a_namEsp'; my $TWO_pfx = 'T'; # The XML Document my $DOC = XML::LibXML->createDocument( "1.0", "UTF-8" ); # Set the root my $root = $DOC->createElement('ABC'); $DOC->setDocumentElement($root); # Establish namespaces $root->setNamespace($ONE_ns, $ONE_pfx, 1); $root->setNamespace($TWO_ns, $TWO_pfx, 0); # Build the document tree my $this = &appendOZ($root); $this = &appendTZ($this); print $DOC->toString(1); sub appendOZ { my $root = shift or die; $root->addNewChild($ONE_ns, 'ZZZ'); return $root; } sub appendTZ { my $root = shift or die; my $this = $root->addNewChild($TWO_ns, 'ZZZ'); &appendOQ($this); return $root; } sub appendOQ { my $root = shift or die; my $this = $root->addNewChild($ONE_ns, 'QWERTY'); &appendTU($this); return $root; } sub appendTU { my $root = shift or die; $root->addNewChild($TWO_ns, 'UIOP'); return $root; }

Where a sub tree is a leaf it says:

my $root = shift or die; $root->addNewChild(<namespace>, <name>); return $root;

Where the sub-tree is the root of another sub-tree itself it says:

sub appendOQ { my $root = shift or die; # Make this node my $this = $root->addNewChild(<namesace>, <name>); # Append sub tree &appendTU($this); return $root; }

This works out nicely and does what I want. If I want a different tree:

<?xml version="1.0" encoding="UTF-8"?> <O:ABC xmlns:O="ONE:" xmlns:T="two:IS:a_namEsp"> <O:ZZZ/> <T:ZZZ> <O:QWERTY> <T:UIOP/> </O:QWERTY> </T:ZZZ> <O:QWERTY> <T:UIOP/> </O:QWERTY> </O:ABC>

I change the core of the programme to:

# Build the document tree my $this = &appendOZ($root); $this = &appendTZ($this); $this = &appendOQ($this); print $DOC->toString(1);

What I expected to be writing, when adding a sub-tree, was some thing like:

# Make this node my $this = $root->addNewChild(<namesace>, <name>); # Append sub tree $this->append(&buildSubTree) return $this;

The only reason (I can see, with my inexperience) to pass the root is for the namespace prefix information. It is nice to use prefixes, and I can see with hindsight that it would require some post processing of the tree to achieve that. So what I was expecting is not the "XML::LibXML" way. Fair enough

So the answer to my question was "No, yes". Simple really

Another thing. Some one said:

I strongly suggest you take the time to read all of its documentation
. Sensible advice. I have been doing exactly that. There are thousands and thousands of lines there, and they are organised as a reference not an introduction. It is hard to start the learning curve with XML::LibXML. In the absence of an introductory document there will be stupid sounding questions from beginners.


In reply to Re^2: Creating Nodes in namespace with XML::LibXML by worik
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.