Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include an unparsed external entity value in a XML?

Tags:

include

xml

cdata

I have an xml syntax error with this example :

<?xml version="1.0"?>
<!DOCTYPE foo [
    <!ENTITY rules SYSTEM "file://data.txt">
]>
<extract>
    <data>&rules;</data>
</extract>

where data.txt contains :

1    <15024
2    >15023

But of course I have syntax error because there are wml special chars in the text file ( < > ). So how can I include these data as unparsed data ?

I hope to have something like this result :

<?xml version="1.0"?>
<extract>
    <data><![CDATA[1    <15024
2    >15023]]></data>
</extract>
like image 432
Xorax Avatar asked Nov 04 '22 20:11

Xorax


1 Answers

Firstly, FYI, what you are referring to is an external parsed general entity, not an unparsed one.

Although your data.txt file does not need to be well-formed itself, it must be well-formed when included. Since a CDATA section does not resolve entities (including external ones), you will have to do the escaping within the external entity file itself (perhaps via a server-side processing file which you can then reference as the external entity file if you cannot or do not wish to do the escaping manually).

If you have control over the file contents and wish to handle the escaping manually, perhaps you may be able to simply surround the text of each file by a CDATA block within the file itself since that should be well-formed when included. (I haven't tried or confirmed, but it seems it should work since again such entities only need to be well-formed when included.)

Alternatively, your other choice would be to escape the < and & characters (You don't normally have to escape >, except ironically, within a CDATA section, in case the text might contain the sequence ]]>--so if you take the CDATA approach, you may need to address this possibility). (Since an external entity can't be used within attributes, you don't need to worry about escaping ' or ", nor did you mention wanting to do this.)

like image 138
Brett Zamir Avatar answered Nov 15 '22 06:11

Brett Zamir