SimpleXMLElement处理addChild和addAttribute中的文本值的基本原理 [英] Rationale behind SimpleXMLElement's handling of text values in addChild and addAttribute

查看:119
本文介绍了SimpleXMLElement处理addChild和addAttribute中的文本值的基本原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一种不一致的行为吗? (PHP 5.2.6)

Isn't that an inconsistent behavior? (PHP 5.2.6)

<?php

$a = new SimpleXMLElement('<a/>');

$a->addAttribute('b', 'One & Two');
//$a->addChild('c', 'Three & Four'); -- results in "unterminated entity reference" warning!
$a->addChild('c', 'Three &amp; Four');
$a->d = 'Five & Six';

print($a->asXML());

渲染器:

<?xml version="1.0"?>
<a b="One &amp; Two">
    <c>Three &amp; Four</c>
    <d>Five &amp; Six</d>
</a>

在bugs.php.net上,他们拒绝了所有关于此的提交,说这是一项功能.为什么会这样呢?顺便说一句,文档中没有关于SimpleXMLElement转义文本值的差异的任何信息.

At bugs.php.net they reject all the submissions about that, saying it's a feature. Why could that possibly be? BTW, there's nothing in the docs about that discrepancy of escaping text values by SimpleXMLElement.

谁能说服我这是可能的最佳API设计决策?

Can anyone convince me it's the best API design decision possible?

推荐答案

只需确保我们在同一页面上,您会遇到三种情况.

Just to make sure we're on the same page, you have three situations.

  1. 使用addAttribute

  1. The insertion of an ampersand into an attribute using addAttribute

使用addChild

The insertion of an ampersand into an element using addChild

通过属性重载将&"号插入元素

The insertion of an ampersand into an element by property overloading

让您感到困惑的是2与3之间的差异.为什么addChild不会自动转义&"符号,而是向对象添加属性并设置其值自动转义&"符号?

It's the discrepancy between 2 and 3 that has you flummoxed. Why does addChild not automatically escape the ampersand, whereas adding a property to the object and setting its value does escape the ampersand automatically?

基于我的直觉,并受到此错误的支持,这是经过深思熟虑的设计决定.属性重载($ a-> d ='5& Six';)被设计为对我来说是转义符". addChild方法的目的是完全添加我告诉您要添加的内容"方法.因此,无论您需要哪种行为,SimpleXML都可以满足您的需求.

Based on my instincts, and buoyed by this bug, this was a deliberate design decision. The property overloading ($a->d = 'Five & Six';) is intended to be the "escape ampersands for me" way of doing things. The addChild method is meant to be "add exactly what I tell you to add" method. So, whichever behavior you need, SimpleXML can accommodate you.

比方说,您有一个文本数据库,其中所有&"号都已被转义.自动转义不适用于您.那就是您要使用addChild的地方.或者说您需要在文档中插入一个实体

Let's say you had a database of text where all the ampersands were already escaped. The auto-escaping wouldn't work for you here. That's where you'd use addChild. Or lets say you needed to insert an entity in your document

$a = simplexml_load_string('<root></root>');
$a->b = 'This is a non-breaking space &nbsp;';
$a->addChild('c','This is a non-breaking space &nbsp;');    
print $a->asXML();

那是该漏洞中的PHP开发人员所倡导的.当您需要在文件中插入&"号而不被转义时,addChild的行为旨在提供不太简单,更可靠"的支持.

That's what the PHP Developer in that bug is advocating. The behavior of addChild is meant to provide a "less simple, more robust" support when you need to insert a ampersand into the document without it being escaped.

当然,这确实使我们处于我提到的第一种情况下,即addAttribute方法. addAttribute方法可以进行转义&"号.因此,我们现在可以将不一致声明为

Of course, this does leave us with the first situation I mentioned, the addAttribute method. The addAttribute method does escape ampersands. So, we might now state the inconsistency as

  1. addAttribute方法转义&符
  2. addChild方法不能逃脱&符
  3. 此行为有些不一致.合理的是,用户希望SimpleXML上的方法以一致的方式进行转义
  1. The addAttribute method escapes ampersands
  2. The addChild method does not escape ampersands
  3. This behavior is somewhat inconsistent. It's reasonable that a user would expect the methods on SimpleXML to escape things in a consistent way

这随后暴露了SimpleXML api的实际问题.理想的情况是

This then exposes the real problem with the SimpleXML api. The ideal situation here would be

  1. 元素对象上的属性重载可以逃脱&符号
  2. 属性对象上的属性重载转义了&符号
  3. addChild方法不会转送&符
  4. addAttribute方法不会转义与号

但这是不可能的,因为SimpleXML没有属性对象的概念. addAttribute方法是(似乎是?)添加属性的唯一方法.因此,事实证明(似乎?)SimpleXML无法使用实体创建属性.

This is impossible though, because SimpleXML has no concept of an Attribute Object. The addAttribute method is (appears to be?) the only way to add an attribute. Because of that, it turns out (seems?) SimpleXML in incapable of creating attributes with entities.

所有这些都揭示了简单 XML的悖论.该API背后的想法是提供一种与复杂的事物进行交互的简单方法.

All of this reveals the paradox of SimpleXML. The idea behind this API was to provide a simple way of interacting with something that turns out to be complex.

该团队可以添加一个SimpleXMLAttribute对象,但这又增加了一层复杂性.如果要多个对象层次结构,请使用DomDoument.

The team could have added a SimpleXMLAttribute Object, but that's an added layer of complexity. If you want a multiple object hierarchy, use DomDoument.

该团队可以在addAttribute和addChild方法中添加标志,但是标志会使API更加复杂.

The team could have added flags to the addAttribute and addChild methods, but flags make the API more complex.

这里真正的教训?也许简单很难,而在截止日期之前简单甚至更难.我不知道是否是这种情况,但是使用SimpleXML似乎有人从一个简单的想法开始(使用属性重载使XML文档的创建变得容易),然后根据出现的问题/功能请求进行调整

The real lesson here? Maybe it's that simple is hard, and simple on a deadline is even harder. I don't know if this was the case or not, but with SimpleXML it seems like someone started with a simple idea (use property overloading to make the creation of XML documents easy), and then adjusted as the problems/feature requests came in.

实际上,我认为这里的真正教训是仅使用JSON;)

Actually, I think the real lesson here is to just use JSON ;)

这篇关于SimpleXMLElement处理addChild和addAttribute中的文本值的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆