You don't provide the input and expected outputs of your code fragment, so it is a little difficult to figure out what you want to do. Plus the code is obviously incorrect as you try to paste the same element ($ds_tag) twice.

Anyway, here is my take on it: I think you want to paste the newly created elements as children of the initial ds element, so you should use first_child or last_child instead of after when you paste them. Generally speaking you should not thing in terms of tags but in terms of elements. I think that's why you are confused and use after. In XML::Twig you process elements, which have one parent, possibly children and siblings. You process them in a tree, and then you can output this tree. You (normally) do not have to care about opening/closing tags, just put the element in the proper place and that will be taken care of by the module.

So here is an attempt at fixing your code. It is probably not what you want but it should help you getting there ;--):

#!/usr/bin/perl -w use strict; use XML::Twig; my $twig=XML::Twig->new( pretty_print => 'indented') ->parse( \*DATA); my @ds= $twig->root->children( 'ds'); foreach my $attr (@ds) { my $ds_elt= XML::Twig::Elt->new( 'ds'); # new XML::Twig::Elt is +deprecated # to create the element as a child use 'last_child', not 'after' # you might want to use 'first_child' instead, I don't know your d +ata $ds_elt->paste( last_child => $attr); # I like using => here my $unknown= "0"; my $unknown_elt= XML::Twig::Elt->new( unknown_sec => $unknown); # you initially tried to paste $ds_elt again # I don't know where you want to paste the element but I guess it' +s here # you might also want to have a look at the insert method that cre +ates # an element as the only child of the calling element, see # http://www.xmltwig.com/xmltwig/twig_dev.html#METHODS_XML_Twig_El +t_insert $unknown_elt->paste( first_child => $ds_elt); my $comment= " PDP Status "; # this is how you create a comment: with an element name '#COMMENT +' my $comment_elt= XML::Twig::Elt->new( '#COMMENT' => $comment); # you could also write $comment_elt->paste( last_child => $ds_elt +); $comment_elt->paste( 'after', $unknown_elt); my $min= "NaN"; my $min_elt= XML::Twig::Elt->new( min => $min); $min_elt->paste( after => $comment_elt); } $twig->print; __DATA__ <doc> <ds><content/></ds> <ds><content/></ds> </doc>

In reply to Re: Another simple XML Twig question by mirod
in thread Another simple XML Twig question by Anonymous Monk

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.