php - 使用 php XSLTProcessor 的 XML/XSLT 输出编码问题

标签 php mysql xml xslt xliff

我正在将一组记录导出到 xml,然后通过 xslt 转换导出到 xliff。导出工作正常,但我无法转换导出文件中的某些字符。这里有一些逐步详细信息:

步骤1.用户输入混合字符串,例如:以下字符串 自动完成打开'看到错误的字符==> í

Mysql db/table字段编码设置为utf8例如

  `unicode longtext COLLATE utf8_unicode_ci`

存储上述文本。

步骤 2. 生成 html 片段以用于导出目的,例如

<html version="1.2">
    <table>
        <tr>
            <td id="Autocomplete_On">Autocomplete On' see the wrong character ==&#62; í</td>
        </tr>
    </table>
    </html>

第3步.转换为xml

  <?xml version="1.0" standalone="yes"?>
     <html version="1.2"><body><table><tr><td id="Autocomplete_On">
        Autocomplete On' see the wrong character ==&gt; &#xC3;&#xAD;</td>
</tr></table></body></html>

第 4 步:使用 xslt 进行转换:

(仅粘贴了所需的输出部分,在浏览器中查看时我看到了这一点,而实际字符是文件中的 à )

 <body>
      <group id="id796986axmarkhtml-0">
        <group id="id533787bxmarkbody-1">
          <group id="id533788bxmarktable-2">
            <group id="id533790bxmarktr-3">
              <trans-unit id="td-4">
                <source>Autocomplete On' see the wrong character ==&gt; í</source>
                <target>Autocomplete On' see the wrong character ==&gt; í</target>
              </trans-unit>
            </group>
          </group>
        </group>
      </group>
    </body>

实际代码:

  private function xml2xliff($htmlStr,$source,$target){
        $xml=new \DOMDocument();
        //hacky way to tidy html
        @$xml->loadHTML($htmlStr);//step 3
        $xsl = new \DOMDocument;
        $xsl->load(__DIR__.'/xliff/xsl/xml2xliff.xsl');
        $proc = new \XSLTProcessor();
        $proc->ImportStyleSheet($xsl);
        $proc->setParameter('', 'source', $this->getIsoName($source));
        $proc->setParameter('', 'target', $this->getIsoName($target));
        return $proc->transformToXML($xml); //step 4
    }

$htmlStr 是步骤 2 中生成的 html 片段,

所以问题是字符串被转换了两次。正在考虑的实际角色是

第 1 步。í

第 2 步。仍然í

第 3 步. 转换为 à,即 í

第 4 步. 转换为 à

另一个例子:

输入。 自动完成功能已消失

xml 输出。 自动完成他们现在已经走了

最佳答案

DOMDocument::loadHtml() 将您的 html 加载为 ANSI,但它是 UTF-8。因此,特殊字符被 split 和破坏。您可以欺骗它使用 UTF-8 和 XML 处理指令:

$html = <<<HTML
<html>
  <table>
    <tr>
      <td id="Autocomplete_On">Autocomplete On' see the wrong character ==&#62; í</td>
    </tr>
  </table>
</html>
HTML;

$dom = new DOMDocument('1.0', 'UTF-8');

$dom->loadHTML('<?xml encoding="UTF-8"?>'.$html);
var_dump(
  $dom->saveXml()
);

输出:

string(331) "<?xml version="1.0" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<?xml encoding="UTF-8"??>
<html version="1.2"><body><table><tr><td id="Autocomplete_On">Autocomplete On' see the wrong character ==&gt; &#xED;</td>&#xD;
    </tr></table></body></html>
"

关于php - 使用 php XSLTProcessor 的 XML/XSLT 输出编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23696238/

相关文章:

mysql - 连接到 mysql 数据库时 ruby​​ 中的连接被拒绝错误

c# - 如何使用.NET XML序列化将对象序列化为单个值

php - Eloquent 查询 AND OR

php - 从文件中获取图像名称的正则表达式

php - PHP/MySQL 的问题

mysql - 使用 mySQL 对两个表中的数字求和

mysql - 查询过滤掉包含html标签的结果

c# - 从动态创建的文本框创建新属性

python - 构建 XML 文档结构图

php - 在 WooCommerce 结账中添加一个自定义复选框,其值显示在管理员编辑顺序中