¿PHP no tiene una función para decodificar la entidad segura para XML? ¿No tienes algún xml_entity_decode?

EL PROBLEMA: Necesito un archivo XML "codificado" por UTF8; es decir, sin una entidad que represente símbolos, todos los símbolos incluidos en UTF8, excepto los únicos 3 que están reservados para XML, "&" (amp), "<" (lt) y ">" (gt). Y,necesito unconstruir en Función que lo hace rápido.: para transformar entidades en caracteres UTF8 reales (sin dañar mi XML).
PD: es un "problema del mundo real" (!); aPMC / revistas, por ejemplo, tienen 2.8 MILLONES de artículos científicos acompañados deun XML DTD especial (Conocido también comoFormato JATS) ... Para procesar como "texto XML-UTF8 habitual" necesitamos cambiar de entidad numérica a char de UTF8.

LA SOLUCIÓN INTENTADA: la función natural de esta tarea eshtml_entity_decode, pero destruye el código XML (!), transformando los 3 símbolos reservados de XML.

Ilustrando el problema

Suponer

  $xmlFrag ='<p>Hello world! &#160;&#160; Let A&lt;B and A=&#x222C;dxdy</p>';

Donde las entidades 160 (nbsp) y x222C (doble integral) deben transformarse en UTF8, y el XML-reservadolt no. El texto XML será (después de transformado),

$ xmlFrag = '<p>¡Hola Mundo! Vamos a&lt;B y A = ∬dxdy</p>';

El texto "A <B" necesita un carácter reservado para XML, por lo que DEBE permanecer comoA&lt;B.

Soluciones frustradas

Trato de usarhtml_entity_decode para resolver (¡directamente!) el problema ... Entonces, actualicé mi PHP a v5.5 para intentar usar elENT_XML1 opción,

  $s = html_entity_decode($xmlFrag, ENT_XML1, 'UTF-8'); // not working
                                                        // as I expected

Quizás otra pregunta es,"¿POR QUÉ no hay otra opción para hacer lo que esperaba?" - Es importante para muchas otras aplicaciones XML (!), no solo para mí.

No necesito unsolución como respuesta ... Ok, muestro mi función fea, tal vez te ayude a entender el problema,

  function xml_entity_decode($s) {
    // here an illustration (by user-defined function) 
    // about how the hypothetical PHP-build-in-function MUST work
    static $XENTITIES = array('&amp;','&gt;','&lt;');
    static $XSAFENTITIES = array('#_x_amp#;','#_x_gt#;','#_x_lt#;');
    $s = str_replace($XENTITIES,$XSAFENTITIES,$s); 

    //$s = html_entity_decode($s, ENT_NOQUOTES, 'UTF-8'); // any php version
    $s = html_entity_decode($s, ENT_HTML5|ENT_NOQUOTES, 'UTF-8'); // PHP 5.3+

    $s = str_replace($XSAFENTITIES,$XENTITIES,$s);
    return $s;
  }  // you see? not need a benchmark: 
     //  it is not so fast as direct use of html_entity_decode; if there 
     //  was an XML-safe option was ideal.

PS: corregido despuésesta respuesta. Debe serENT_HTML5 bandera, para convertirrealmente todas las entidades nombradas.

Respuestas a la pregunta(5)

Su respuesta a la pregunta