我正在修改由其他库生成的XML文件的内容。 我正在使用PHP(5.3.10)进行一些DOM修改并重新插入替换节点。
我正在使用的XML数据有" 在我进行操作之前的元素,并且当我完成修改时,我想按照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不应该转换为" 实体就像& 行为正常。 这个$ temp字符串应该输出,因为它最初是在saveXML()期间与实体一起输入的。
$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 like & $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" &.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 " 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 " elements. See my example.
$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 "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 " entities just like the & which behaves properly. This $temp string should really be output as it is initially entered with the entities during saveXML().
$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 like & $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" &.最满意答案
答案是根据规范它实际上并不需要任何转义(跳过CDATA的提及):
&符号(&)和左尖括号(<) 不得以其字面形式出现(...)如果在别处需要它们,则必须使用数字字符引用或字符串" & "和" < "转义" < " 。 右尖括号(>) 可以使用字符串" > " (...)
为了允许属性值包含单引号和双引号,撇号或单引号字符(')可以表示为" ' " ,而双引号字符(“)表示为" " " " " " 。
您可以使用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: <, ampersand: &, closing tag: ></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 " & " and " < " respectively. The right angle bracket (>) may be represented using the string " > " (...)
To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " ' ", and the double-quote character (") as " " ".
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: <, ampersand: &, closing tag: ></description>更多推荐
发布评论