Bug #15092 Updated: xml_parse() fails if XML-data contains entity like   or © ...

From: Date: Tue, 22 Jan 2002 23:46:43 +0000
Subject: Bug #15092 Updated: xml_parse() fails if XML-data contains entity like   or © ...
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-77843@lists.php.net to get a copy of this message
ID: 15092
User updated by: bs_php@infeer.com
Reported By: bs_php@infeer.com
Status: Closed
Bug Type: XML related
Operating System: Win 2k (all I gues)
PHP Version: 4.1.0
New Comment:

Don't take following personal:
I think it's terrible habit to miss out practical functionality and
point to a spec and say "It must be this way, the spec says so". 
I mean is there a *real* reason not to support the ISO latin-1
entities??
PHP slogan is to keep simple stuff simple, isn't it?
So is there a chance that the ISO latin-1 entities can be turned on in
future (by parameter or so)?



Previous Comments:
------------------------------------------------------------------------

[2002-01-22 18:08:59] chregu@php.net

It does not work, because the XML-standard (see
http://www.w3.org/TR/1998/REC-xml-19980210#sec-predefined-ent)
does not
say, that an XML Parser should understand latin1-entities. The expat
Parsers behaves like it is expected (sablotron and domxml for example
do the same, they don't understand latin1-entities without external
entities..). Understanding the iso8859-1 charset is something
completely different and has not much to do with entities (in the
context of the xml-standard..).

I agree with you, that it should be easier to include external
entities, but the way ext/xml does is also just like a SAX-parser
should do it -> do everything by yourself with callback functions... (i
assume, that was the idea :) )

------------------------------------------------------------------------

[2002-01-22 16:49:08] bs_php@infeer.com

Yes, that's also a possibility. 
  Not practical, 
  not easy, 
  not fancy, 
  not efficient... but a possibility.

---

Still, I think it's a bug:
According to the manual, PHP's XML parser *handles* ISO 8859-1
(Latin-1) as default. That means XML source may contain ü,ö,ä,è, a.s.o.

So way should it fail when I intend to use the latin1-entities!? I see
no reason way latin1-entities shouldn't work per default too!!

------------------------------------------------------------------------

[2002-01-22 11:34:20] chregu@php.net

This doesn't work, because the default entities are only:
<!ENTITY lt     "&#38;#60;"> 
<!ENTITY gt     "&#62;"> 
<!ENTITY amp    "&#38;#38;"> 
<!ENTITY apos   "&#39;"> 
<!ENTITY quot   "&#34;"> 

For the latin1-entities to work, you have to set an external entity to

http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent
(or some local file with that content)
and then set up a xml_set_external_entity_ref_handler().
See the details about that in the manual.

(set to feedback, 'cause I didn't really test it, if someone can verify
that, we can close it.)


------------------------------------------------------------------------

[2002-01-22 09:49:09] bs_php@infeer.com

After some more testes I found that the only literal entities that work
are:  &amp;, &lt; &gt; and &quot;. 
*ALL* others (like &nbsp; &copy; a.s.o.) cause an
XML_ERROR_UNDEFINED_ENTITY error.

The best work around to this problem, is to tranlate the entities 
found in the XML source to theire numeric equivalent E.g. &nbsp; to
&#160; / &copy; to &#169; a.s.o.
Following function will do the job:

  /**
  * Translate literal entities to their numeric equivalents and vice
versa.
  *
  * PHP's XML parser (in V 4.1.0) has problems with entities! The only
one's that are recognized
  * are &amp;, &lt; &gt; and &quot;. *ALL* others (like &nbsp; &copy;
a.s.o.) cause an 
  * XML_ERROR_UNDEFINED_ENTITY error. I reported this as bug at
http://bugs.php.net/bug.php?id=15092
  * The work around is to translate the entities found in the XML
source to their numeric equivalent
  * E.g. &nbsp; to &#160; / &copy; to &#169; a.s.o.
  * 
  * NOTE: Entities &amp;, &lt; &gt; and &quot; are left 'as is'
  * 
  * @author Sam Blum bs_php@users.sourceforge.net
  * @param string $xmlSource The XML string
  * @param bool   $reverse (default=FALSE) Translate numeric entities
to literal entities.
  * @return The XML string with translatet entities.
  */
  function _translateLiteral2NumericEntities($xmlSource, $reverse =
FALSE) {
    static $literal2NumericEntity;
    
    if (empty($literal2NumericEntity)) {
      $transTbl = get_html_translation_table(HTML_ENTITIES);
      foreach ($transTbl as $char => $entity) {
        if (strpos('&"<>', $char) !== FALSE) continue;
        $literal2NumericEntity[$entity] = '&#'.ord($char).';';
      }
    }
    if ($reverse) {
      return strtr($xmlSource, array_flip($literal2NumericEntity));
    } else {
      return strtr($xmlSource, $literal2NumericEntity);
    }
  }





------------------------------------------------------------------------

[2002-01-17 21:03:07] bs_php@infeer.com

PHP XML-parser has problems with the full iso8859-1 char set when
trying to use entity names. E.g. the parser will fail with "undefined
entity" if the XML data you parse contains &nbsp; or &copy; a.s.o.
(there many more).

Some entities do work, like &lt; &gt; &amp; as well as the alternative
notation unsing the ISO-code number: like non-breaking space  === 
&#160;

For a full iso8859-1 list and it's entities see:
http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html

Here's the test script you can use to check the error :
<?php
$xmlString[0] = "<AAA>&#160;</AAA>";
$xmlString[1] = "<AAA>&nbsp;</AAA>";

  function startElement($xml_parser, $name, $attrs) {}
  function endElement($xml_parser, $name) {}
  function characterData($xml_parser, $text) {echo "Handling character
data: '".htmlspecialchars($text)."'<br>";}
  
  $xml_parser = xml_parser_create();
  xml_set_element_handler($xml_parser, "startElement", "endElement");
  xml_set_character_data_handler($xml_parser,  "characterData");
  
  // Parse the XML data.
  if (!xml_parse($xml_parser, $xmlString[1], TRUE)) {
   echo "XML error in given {$source} on line ".
xml_get_current_line_number($xml_parser) . 
        '  column ' . xml_get_current_column_number($xml_parser) .
        '. Reason:' .
xml_error_string(xml_get_error_code($xml_parser));
  }
?>



------------------------------------------------------------------------



Edit this bug report at http://bugs.php.net/?id=15092&edit=1



Thread (7 messages)

« previous php.dev (#77843) next »