XML 使用 PHP 在 XML 元素中添加编码的 XHTML

xml add encoded xhtml in xml element using php

提问人:Premlatha 提问时间:1/13/2022 更新时间:1/14/2022 访问量:205

问:

我想创建嵌入编码的xhtml的xml文件。我单独编码了xhtml文件。在创建xml元素时,我想在xml元素中添加xhtml的编码内容。在我添加并最终输出到浏览器后,浏览器中显示错误。test

此页面包含以下错误: 第 9 行第 144 列的错误:编码错误 下面是页面的渲染,直到第一个错误。

    <?php    
     $dom                   =new DOMDocument('1.0','utf-8');
     $content = (file_get_contents("test_xmlencoding.xhtml"));
     $element = $dom->createElement('test', $content);
     $dom->appendChild($element);
     header('Content-type: text/xml;');
     echo $dom->saveXML();    
    ?>

XHTML 文件

&lt;?xml version="1.0" ?&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta content="TX21_HTM 21.0.406.501" name="GENERATOR" /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body style="font-family:'Arial';font-size:12pt;text-align:left;"&gt;
&lt;p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"&gt;&lt;span style="font-family:'Verdana';font-size:9pt;"&gt;ABC1.&lt;/span&gt;&lt;/p&gt;
&lt;p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"&gt;&lt;span style="font-family:'Verdana';font-size:9pt;"&gt;(ABC2)&lt;/span&gt;&lt;/p&gt;
&lt;p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"&gt;&lt;span style="font-family:'Verdana';font-size:9pt;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"&gt;&lt;span style="font-family:'Verdana';font-size:9pt;"&gt;ABC3&lt;/span&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;

在不编码的情况下添加 xhtml 内容时,输出在浏览器上呈现不会出错。

我已尝试替换

$content = (file_get_contents("test_xmlencoding.xhtml")); 

$content = htmlentities(file_get_contents("test_xmlencoding.xhtml")); 

输出仅显示测试元素的结束标记 。</test>

PHP XML XHTML 编码

评论

0赞 CBroe 1/13/2022
请澄清一下,您所说的“XHTML 文件”实际上是 XHTML - 还是只是编码文本,如您所示的示例?它测试了两个版本的代码,如所示,生成的 XML 在这两种情况下都通过了格式检查。
0赞 Premlatha 1/13/2022
它被编码为 XHTML。我想将其附加到xml元素。只有当我删除>和<之间的空间时,才起作用。encoded xhtml &gt;&lt;
0赞 CBroe 1/13/2022
我无法重现您的问题。如果我将 3v4l.org/MPQGc 的输出放在 xmlvalidation.com,它会说一切都很好,没有错误。
0赞 CBroe 1/13/2022
你实际上想通过这个实现什么?将此内容放在 CDATA 部分中是否更有意义?php.net/manual/en/domdocument.createcdatasection.php
0赞 Premlatha 1/13/2022
我尝试过XHTML的不同部分。它在这里工作,3v4l.org/Uer9u#v7.2.30。但是,我的网站显示错误,我删除了每个之间的空格,然后它运行没有错误This page contains the following errors: error on line 8 at column 78: Encoding error Below is a rendering of the page up to the first error.&gt; &lt;

答:

0赞 ThW 1/14/2022 #1

和属性的第二个参数只有部分转义。他们希望特殊字符已经作为实体进行转义 - 除了 和 。DOMDocument::createElement()DOMNode::$nodeValue<>

$document = new DOMDocument();
$document->appendChild(
  $tests = $document->createElement('tests')
);
$tests
  ->appendChild($document->createElement('test', 'a < b'));
$tests
  ->appendChild($document->createElement('test', 'a & b'));
echo $document->saveXML();

输出:

Warning: DOMDocument::createElement(): unterminated entity reference b in ... on line 9
<?xml version="1.0"?>
<tests><test>a &lt; b</test><test/></tests>

method 参数不是 DOM 标准的一部分,并且该属性的行为与规范不同。

在原始 DOM 中,您期望将内容添加为单独的文本节点。这也允许混合子节点。现代 DOM 引入了充当快捷方式的属性。DOMNode::$textContent

下面是一个示例:

$xhtml = <<<'XHTML'
<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <em>a &amp; b</em>
  </body>
</html>
XHTML;

$document = new DOMDocument();
$document->appendChild(
  $tests = $document->createElement('tests')
);
// append child element and set its text content
$tests
  ->appendChild($document->createElement('test'))
  ->textContent = $xhtml;
// append child element, then append child text node
$tests
  ->appendChild($document->createElement('test'))
  ->appendChild($document->createTextNode($xhtml));  
  
$document->formatOutput = true;
echo $document->saveXML();

输出: 注意双重转义。&amp;amp;

<?xml version="1.0"?>
<tests>
  <test>&lt;?xml version="1.0" ?&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;body&gt;
    &lt;em&gt;a &amp;amp; b&lt;/em&gt;
  &lt;/body&gt;
&lt;/html&gt;</test>
  <test>&lt;?xml version="1.0" ?&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;body&gt;
    &lt;em&gt;a &amp;amp; b&lt;/em&gt;
  &lt;/body&gt;
&lt;/html&gt;</test>
</tests>

评论

0赞 Premlatha 1/24/2022
此方法有助于在附加 xml 元素后自动对 xhtml 的实体进行编码。它帮助了我。谢谢。现在,我找到了 xhtml 的解码实体,因为我的源文档是编码的 xhtml 实体。
0赞 ThW 1/24/2022
如果将源代码加载为 HTML,则 DOM 将对命名实体进行解码。3v4l.org/PgeeM#v8.1.2 - 否则它只会解码数字实体。
0赞 Premlatha 1/25/2022
我在加载的 xhtml 内容上使用 utf_decode() 来删除 [它出现在空白处]。然后,我将“\r\n”、“\r”替换为“\n”,因为出现在每行的末尾。 谢谢。Â&#13;$doc>loadHTML($html); $decoded_content=utf8_decode($doc->documentElement->textContent); $normalisdlf_1 =str_replace(array("\r\n", "\r"), "\n", $decoded_content);
0赞 ThW 1/25/2022
与其解码 UTF-8,不如考虑将其用作所有内容的默认编码。 将 UTF-8 转换为拉丁语 1,拉丁语 1 中不可用的任何字符都将丢失/损坏。因此,除了在极少数情况下,例如为其他不理解它的系统导出,请始终使用 utf-8。要规范化文本,请检查 ext/intl 音译器 (stackoverflow.com/a/51602898/497139)。utf8_decode()
0赞 Premlatha 1/26/2022
当我想使用音译器时,它要求在使用前安装 PECL 扩展。我没有继续。我尝试用空字符串替换不间断空格 &nbsp [以 utf-8 编码:“\xc2\xa0”]。该 Â 不再出现在输出文件中。$removenonbreakingspace_1=str_replace("\xc2\xa0",' ',$xhtml); $doc->loadHTML($removenonbreakingspace_1); $decoded_content_1=$doc->documentElement->textContent; $final=str_replace(array("\r\n", "\r"), "\n", $decoded_content_1);