PHP如何使用“;(PHP How to use quot; entities in XML with DOMdocument)

编程入门 行业动态 更新时间:2024-10-08 13:34:26
PHP如何使用“;(PHP How to use quot; entities in XML with DOMdocument)

我正在修改由其他库生成的XML文件的内容。 我正在使用PHP(5.3.10)进行一些DOM修改并重新插入替换节点。

我正在使用的XML数据有&quot 在我进行操作之前的元素,并且当我完成修改时,我想按照http://www.w3.org/TR/REC-xml/保留这些元素。

但是我遇到了PHP改变"问题" 元素。 看看我的例子。

$temp = 'Hello "XML".'; $doc = new DOMDocument('1.0', 'utf-8'); $newelement = $doc->createElement('description', $temp); $doc->appendChild($newelement); echo $doc->saveXML() . PHP_EOL; // shows " instead of element $node = $doc->getElementsByTagName('description')->item(0); echo $node->nodeValue . PHP_EOL; // also shows "

产量

<?xml version="1.0" encoding="utf-8"?> <description>Hello "XML".</description> Hello "XML".

这是PHP错误还是我做错了什么? 我希望没有必要在每个char位置使用createEntityReference。

类似问题: PHP XML实体编码问题


编辑:作为一个示例,显示saveXML不应该转换为&quot; 实体就像&amp; 行为正常。 这个$ temp字符串应该输出,因为它最初是在saveXML()期间与实体一起输入的。

$temp = 'Hello &quot;XML&quot; &amp;.'; $doc = new DOMDocument('1.0', 'utf-8'); $newelement = $doc->createElement('description', $temp); $doc->appendChild($newelement); echo $doc->saveXML() . PHP_EOL; // shows " instead of element like &amp; $node = $doc->getElementsByTagName('description')->item(0); echo $node->nodeValue . PHP_EOL; // also shows " &

产量

<?xml version="1.0" encoding="utf-8"?> <description>Hello "XML" &amp;.</description> Hello "XML" &.

I am working on modifying the contents of an XML file generated by some other library. I'm making some DOM modifications with PHP (5.3.10) and reinserting a replacement node.

The XML data I'm working with has &quot; elements before I do the manipulation and I want to keep those elements as per http://www.w3.org/TR/REC-xml/ when I'm done with the modifications.

However I'm having problems with PHP changing the &quot; elements. See my example.

$temp = 'Hello &quot;XML&quot;.'; $doc = new DOMDocument('1.0', 'utf-8'); $newelement = $doc->createElement('description', $temp); $doc->appendChild($newelement); echo $doc->saveXML() . PHP_EOL; // shows " instead of element $node = $doc->getElementsByTagName('description')->item(0); echo $node->nodeValue . PHP_EOL; // also shows "

Output

<?xml version="1.0" encoding="utf-8"?> <description>Hello "XML".</description> Hello "XML".

Is this a PHP error or am I doing something wrong? I hope it isn't necessary to use createEntityReference in every char location.

Similar Question: PHP XML Entity Encoding issue


EDIT: As an example to show saveXML should not be converting the &quot; entities just like the &amp; which behaves properly. This $temp string should really be output as it is initially entered with the entities during saveXML().

$temp = 'Hello &quot;XML&quot; &amp;.'; $doc = new DOMDocument('1.0', 'utf-8'); $newelement = $doc->createElement('description', $temp); $doc->appendChild($newelement); echo $doc->saveXML() . PHP_EOL; // shows " instead of element like &amp; $node = $doc->getElementsByTagName('description')->item(0); echo $node->nodeValue . PHP_EOL; // also shows " &

Output

<?xml version="1.0" encoding="utf-8"?> <description>Hello "XML" &amp;.</description> Hello "XML" &.

最满意答案

答案是根据规范它实际上并不需要任何转义(跳过CDATA的提及):

&符号(&)和左尖括号(<) 不得以其字面形式出现(...)如果在别处需要它们,则必须使用数字字符引用或字符串" &amp; "和" &lt; "转义" &lt; " 。 右尖括号(>) 可以使用字符串" &gt; " (...)

为了允许属性值包含单引号和双引号,撇号或单引号字符(')可以表示为" &apos; " ,而双引号字符(“)表示为" &quot; " " &quot; " 。

您可以使用createTextNode()轻松验证这一点,以执行正确的转义:

$dom = new DOMDocument; $e = $dom->createElement('description'); $content = 'single quote: \', double quote: ", opening tag: <, ampersand: &, closing tag: >'; $t = $dom->createTextNode($content); $e->appendChild($t); $dom->appendChild($e); echo $dom->saveXML();

输出:

<?xml version="1.0"?> <description>single quote: ', double quote: ", opening tag: &lt;, ampersand: &amp;, closing tag: &gt;</description>

The answer is that it doesn't actually need any escaping according to the spec (skipping the mentions of CDATA):

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form (...) If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; " (...)

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

You can verify this easily by using createTextNode() to perform the correct escaping:

$dom = new DOMDocument; $e = $dom->createElement('description'); $content = 'single quote: \', double quote: ", opening tag: <, ampersand: &, closing tag: >'; $t = $dom->createTextNode($content); $e->appendChild($t); $dom->appendChild($e); echo $dom->saveXML();

Output:

<?xml version="1.0"?> <description>single quote: ', double quote: ", opening tag: &lt;, ampersand: &amp;, closing tag: &gt;</description>

更多推荐

本文发布于:2023-07-27 04:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   quot   PHP   DOMdocument   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!