cmccutcheon@oneil.com via RT wrote:
Show quoted text> Queue: XML-Twig
> Ticket <URL:
http://rt.cpan.org/Ticket/Display.html?id=18944 >
>
> Greetings,
>
> Thank-you for the fix! Once you explained #CDATA is a full fledged
> element, I understood why XML::Twig was behaving the way it was. The
> misunderstanding was on my part; mainly because I was expecting
> ->set_cdata to abstract away the details of working with character text
> in XML (sort of how ->set_text does).
It does now.
Show quoted text> Maybe this could be an extension to how ->set_text works. That is, if
> ->set_text is called with only a string it behaves as it currently does.
> But if ->set_text is called with something like ->set_text('#CDATA' =>
> $string), then it would create a #CDATA element instead of a #PCDATA
> element. On a slight tangent (and out of sheer curiosity), is there a
> historical reason for ->set_text to cut all the children out of an
> element and create a single #PCDATA element?
With the change, set_cdata works in a way that's very similar to
set_text, so that should be OK.
And #CDATA and #PCDATA are separate elements because of mixed content:
in XHTML for example, a single p element like '<p>foo <b>bar</b>
baz</p>' contains 3 children, 2 #PCDATA (text nodes in the DOM) and 1 b
element.
Show quoted text>
> The reason this behavior was/is needed is because I'm creating an
> element much earlier in a process with no knowledge of what is going to
> get stuffed into it. Today, armed with my new-found knowledge of #CDATA
> being a real element, I also tried:
>
> my $elt = XML::Twig::Elt->new("qux");
> my $data = XML::Twig::Elt->new('#CDATA' => "test this '<' & this '>'");
> $data->paste($elt);
> $elt->paste('last_child', $root);
> print $root->sprint, "\n";
>
> which works; but the above code seems a little verbose just to declare a
> specific section of text in an element as being #CDATA. Thank-you for
> sending DWIM to the rescue; now #CDATA is almost as transparent to work
> with as #PCDATA :D .
I have just one question: do you really need the CDATA section? When you
print (or sprint, or flush) the twig, special characters will be
automatically escaped, so you can write
my $elt = XML::Twig::Elt->new(qux => "test this '<' & this '>'");
and when you print the element you will get valid XML:
<qux>test this '<' & this '>'</qux>
A parser reading this will send nearly the same information to the
application than with the CDATA version.
Hope that helps
--
Michel Rodriguez
Perl & XML
xmltwig.com