Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

XML::Twig - setting text and elements

by slugger415 (Monk)
on Sep 08, 2015 at 21:34 UTC ( [id://1141371]=perlquestion: print w/replies, xml ) Need Help??

slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have some XML such as this, where the linktext element contains a combination of text and other elements:

<citreference id="mycite"> <linktext><keyword conref="ds"/> support for IPv6 addresses</linktext> </citreference>

I can capture this with XML::Twig by creating an array, and referencing it thusly, where $citref is my handler:

my(@children) = $citref->first_child('linktext')->children; $linktext{$citref->att('id')} = \@children;

Simple enough. The question I'm having is how to extract that into another element elsewhere? I couldn't find that in the documentation. Something like:

my($h) = $linktext{'xxx'}; # the xxx is the appropriate id for the has +h $newcontext->set_text(\@$h); # $newcontext is 'xref' in this case

That doesn't work right, but I'm getting something in the output:

<xref>ARRAY(0x4128670)</xref>

I suspect the $e->set_text() function only applies to text (not elements) and I suspect if there's more than one text segment each will clobber the previous. Hence I'm cautious about iterating through the array.

Any tips on how to do this? Thank you.

Replies are listed 'Best First'.
Re: XML::Twig - setting text and elements
by Anonymous Monk on Sep 08, 2015 at 23:16 UTC
    What is the xml that you want to create? (yes I realize there is maybe a word description of what you want, but actual xml is easier to interpret)

      So dom style http://xmltwig.org

      #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $t = XML::Twig->new( pretty_print => 'indented' ); $t->xparse(q{<roooooo> <citreference id="mycite"> <linktext> <keyword conref="ds"/> support for IPv6 addresses</linktext> </citreference> <moops></moops> </roooooo>}); $t->print; my( @links ) = $t->findnodes( '//linktext' ); my( $moops ) = $t->findnodes( '//moops' ); for my $link ( @links ){ print $link->path, "\n"; $link->cut->paste( $moops ); } $t->print; __END__ <roooooo> <citreference id="mycite"> <linktext><keyword conref="ds"/> support for IPv6 addresses</linkt +ext> </citreference> <moops></moops> </roooooo> /roooooo/citreference/linktext <roooooo> <citreference id="mycite"></citreference> <moops> <linktext><keyword conref="ds"/> support for IPv6 addresses</linkt +ext> </moops> </roooooo>

        Thank you for the suggestion. My solution was this:

        $topicref->set_content(@$h);

      Sorry i wasn't clear about that. I'd like everything inside of linktext to go inside of xref.

      <xref><keyword conref="ds"/> support for IPv6 addresses</xref>

      I also didn't mention there might be other elements midway.

      <linktext><keyword conref="ds"/> support for <b>IPv6</b> addresses</li +nktext>

      So I need to iterate through the @children array and handle each child, which is either text or an element.

      thanks, will try these other suggestions.

Re: XML::Twig - setting text and elements
by Anonymous Monk on Sep 09, 2015 at 14:29 UTC
    Try $newcontext->paste(last_child => $_) foreach @{$linktext{'xxx'}} (written from a phone, code is untested). Paste() is XML::Twig::Elt's method.

      tried that, but getting this:

      cannot paste an element that belongs to a tree at C:\scripts\keyref2href.pl line 259.

      line 259:

      $topicref->paste(last_child=>$_);

      seems to be crashing on the $_ that contains the reference to the element. Any thoughts?

      Thank you.

        Any thoughts?

        You can't paste what has not been cut?

        Either $_->copy or $_->cut the element you are trying to paste.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1141371]
Approved by stevieb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found