📜 ⬆️ ⬇️

Loading into XML a string containing entity references (& nbsp; etc.)

Task
In a system that uses XSLT templates, give contentrs the ability to simply use their usual entity names like & nbsp; & mdash; etc. without the need for digital definitions like & # 160 ;, & # x00A0; or placing special characters in CDATA

Problem
When loading multiple blocks into XML from the database, I had the following problem: The parser cursed undescribed entities in the following way: “Entity: line 1: parser error: Entity 'yen' not defined”.

Decision
In order to avoid such a problem when loading a string with a description of entities, you need to do what it expects from us, i.e. describe these entities.

The loaded string should look something like this:
<! DOCTYPE root [
<! ENTITY nbsp "& # 160;">
<! ENTITY yen "& # 165;">
]>
<text> Angry & nbsp; text with evil entities & yen; </ text>

')
In order not to practice ten times, you can pick up a file with a list of all the necessary names once
$ entities = file_get_contents (PATH_SYS. 'templates / symbols.ent');


It works approximately as follows:

Prepare a string for download
$ block_content = '<! DOCTYPE root ['. $ entities. ']> <text>'. $ block_content. '</ text>';


Using the DOMDocument :: loadXML method:
$ dom_child_doc = DOMDocument :: loadXML ($ block_content, LIBXML_NOENT);
$ block_content_node = $ blockDoc-> importNode ($ dom_child_doc-> lastChild, true);
$ block = $ blockDoc-> blocks-> appendChild ($ block_content_node);


Using the simplexml_load_string function:
$ xml = simplexml_load_string ($ block_content, 'SimpleXMLElement', LIBXML_NOENT);
$ dom_sxe = dom_import_simplexml ($ xml);
$ block_content_node = $ blockDoc-> importNode ($ dom_sxe, true);
$ block = $ blockDoc-> blocks-> appendChild ($ block_content_node);

$ blockDoc is a kind of DOMDocument object in which we add blocks.
$ blockDoc-> blocks - the node of this document, directly into which we place all the blocks
You need to remember to set the “LIBXML_NOENT - convert entities” option for the string loader, if there is a need for all entity names to be converted directly to the characters that we need.

Source: https://habr.com/ru/post/30175/


All Articles