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


In reply to 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.