Update:
This behavior is correct as per the source code.
(Sure wish there was an explanatory blurb explaining that XML::Twig::Elt->new() removes the '#CDATA' key in an attribute hashref. C'est la vie.)
Thank you haukex and Anon Monk for your explanation and workarounds!
Good afternoon!
I am experiencing a rather unexpected issue creating new elements with XML::Twig's XML::Twig::Elt->new() method where the '#CDATA' key in my attribute hashref gets undefined after passing it to new().
e.g.:
my $attributes = $sibling_element->atts();
$attributes->{'#CDATA'} = 1;
# '#CDATA' attribute is defined here
my $elt1 = XML::Twig::Elt->new('present', $attributes, 'This is in CDA
+TA');
# '#CDATA' attribute is undefined here
my $elt2 = XML::Twig::Elt->new('missing', $attributes, 'This is not in
+ CDATA');
Is this supposed to happen? I did not see anything mentioning this in the CPAN docs...
Thank you for your time.
Perl info:
perl -v
This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x
+64-multi-thread
XML::Twig info:
3.52
Sample code:
#!/usr/bin/perl
use 5.026;
use strict;
use warnings;
use XML::Twig;
{
open (my $OFILE, '>:utf8', 'file.out.xml') or die "$!\n$^E";
my $t = XML::Twig->new(
twig_handlers => {
'/keys/key' => sub { makeDuplicates(@_, $OFILE); 1; },
},
output_encoding => 'utf-8',
pretty_print => 'indented',
);
$t->safe_parse(\*DATA);
if ( $@ ) {
die "Error occured in XML data\n$@";
}
close $OFILE;
}
sub makeDuplicates {
my ($_twig, $key_element, $OFILE) = @_;
my $text = $key_element->text();
my $attributes = $key_element->atts();
$attributes->{'#CDATA'} = 1;
# '#CDATA' attribute is defined here
my $lc_element = XML::Twig::Elt->new('lc_key', $attributes, lc $text
+);
# '#CDATA' attribute is undefined here
my $uc_element = XML::Twig::Elt->new('uc_key', $attributes, uc $text
+);
$lc_element->paste(after => $key_element);
$uc_element->paste(after => $key_element);
$_twig->flush($OFILE);
return;
}
__DATA__
<?xml version="1.0" encoding="utf-8"?>
<keys>
<key order='01'><![CDATA[One hen]]></key>
<key order='02'><![CDATA[Two ducks]]></key>
<key order='03'><![CDATA[Three squawking geese]]></key>
<key order='04'><![CDATA[Four limerick oysters]]></key>
<key order='05'><![CDATA[Five corpulent porpoises]]></key>
<key order='06'><![CDATA[Six pair of Don Alverzo's tweezers]]></key>
<key order='07'><![CDATA[Seven thousand Macedonians in full battle a
+rray]]></key>
<key order='08'><![CDATA[Eight brass monkeys from the ancient sacred
+ crypts of Egypt]]></key>
<key order='09'><![CDATA[Nine apathetic, sympathetic, diabetic old m
+en on roller skates, with a marked propensity towards procrastination
+ and sloth]]></key>
<key order='10'><![CDATA[Ten lyrical, spherical, diabolical denizens
+ of the deep who stalk about the corners of the cove all at the same
+time.]]></key>
</keys>
Desired output:
Actual output:
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.