Subject: | CDATA including quotes inside an attribute |
Date: | Sat, 10 Apr 2010 13:10:44 -0400 |
To: | bug-XML-Tiny [...] rt.cpan.org |
From: | Nick Dumas <nick.dumas [...] gmail.com> |
Recently I was again trying to add some perl code to an attribute in
the XML, and I was getting "Not Well Formed" despite using CDATA tags
to block it off.
After some investigation, the parser was tripping over the unreplaced
' in "This ship's name". (see sample below)
I just added ' and " to the CDATA->PCDATA substitution code, and now
it parses happily.
(Covers the 5 reserved characters)
http://www.w3schools.com/tags/ref_entities.asp
There is a note there that IE doesn't like ' so it might be good
to replace that with ' instead.
On the other hand, the code is reading, not writing, so IE shouldn't
be involved.
EG:
=============
<object id='SensorProbe'>
<attribute name='object type'>
<value type='scalar' value='ship'/>
</attribute>
<attribute name='AIscript'>
<value type='scalar' value='<![CDATA[
# Inputs
# ~~~~~~
# $self = This ship's name/ID
# $library = Hash tree of the mod library.
... ]]> '/>
...
=========
Change to XML-tiny: (Line ~190 in tiny.pm)
=========
# turn CDATA into PCDATA
$file =~ s{<!\[CDATA\[(.*?)]]>}{
$_ = $1.chr(0); # this makes sure that empty CDATAs become
s/([&<>'"])/ # the empty string and aren't just
thrown away.
$1 eq '&' ? '&' :
$1 eq '<' ? '<' :
$1 eq '"' ? '"' :
$1 eq "'" ? ''':
'>'
/eg;
$_;
}egs;
=========
- Nick (SuicideJunkie on PerlMonks)