SimpleXML 具有 xmlns:xmlns="" 声明- 无法删除
太气人了,简直说不出话来。我已经用 SimpleXML 组装了一个 RSS 提要,但它正在使用名称空间,这是现在的名称空间。但是,在输出时,它会不断尝试在根节点中声明 xmlns:xmlns="" 。尽管我不做这样的事。
它开始于此
$rssXML->addAttribute("version", '2.0');
$rssXML->addAttribute("xmlns:media", "http://search.yahoo.com/mrss/", '');
$rssXML->addAttribute("xmlns:dcterms", "http://purl.org/dc/terms/", '');
,之后我这样做:-
header("Content-Type: application/rss+xml");
echo $syndicationXML->asXML();
但它输出:-
<?xml version="1.0"?>
<rss xmlns:xmlns="" version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/"><channel>...
我不理解所有这些名称空间声明。这是怎么回事?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SimpleXML 的问题在于它的
addAttribute
函数添加了一个属性,而不是一个命名空间,虽然它看起来像您想要的那样,但它并不意味着按照您使用它的方式使用。它的目的是添加一个属于特定命名空间(指定为第三个参数)的值,而不是添加命名空间本身。之所以最终得到
xmlns:xmlns
是因为 SimpleXML 发现您在指定名称xmlns:media
时使用了xmlns
命名空间因此它创建了一个空的xmlns:xmlns
。以下是您问题的 2 个解决方案:
1。在构造函数中的命名空间中指定。
2.使用
preg_replace
替换xmlns:xmlns=""
The problem with SimpleXML is that it's
addAttribute
function adds an attribute, not a namespace and although it seems like it does what you want, it's not meant to be used the way you are using it.It's meant to add a value that's part of a particular namespace (specified as the third parameter), not to add the namespace itself. The reason why you end up with
xmlns:xmlns
is because SimpleXML found that you used thexmlns
namespace when specifying the namexmlns:media
for instance so it created an emptyxmlns:xmlns
.Here are 2 solutions to your problem:
1. Specify in the namespaces in the constructor.
2. Replace
xmlns:xmlns=""
usingpreg_replace
您写道您想删除它。最好的办法就是不要把它放在第一位。
这通过使用
xmlns:
前缀(另一次)来实现 - 它以某种方式在 simplexml 中启用特殊的操作模式 - 并且不提供一个空的命名空间-URI(否则你实际上会问添加这一点):这将创建以下输出(为了您的阅读乐趣而美化):
这个“作弊”最初在 “无法使用 PHP Simplexml 添加具有命名空间前缀的属性”,但此处缺失。
You wrote that you want to remove that. Best way is to not put it in the first place.
This works by prefixing with
xmlns:
(another time) - it somehow enables a special mode of operation in simplexml - and not providing an empty namespace-URI (otherwise you actually ask for adding that):This creates the following output (beautified for your reading pleasure):
This "cheat" was originally hinted in "Unable to add Attribute with Namespace Prefix using PHP Simplexml" but was missing here.
SimpleXMLElement
只能间接添加命名空间。由于您只能添加元素和属性节点,而不能添加名称空间声明、文本节点或其他节点类型,因此您必须以某种方式使用SimpleXMLElement->addAttribute()
或SimpleXMLElement->addChild()
。后者只会向子级添加新的命名空间,因此这里没有用。addAttribute()
会将命名空间和属性添加到给定元素,因此,如果您随后删除该属性,则会留下所需的命名空间。没有明显的方法来删除属性,但可以使用
unset
,如“在 SimpleXML for PHP 中删除具有特定属性的子项" 可以使用SimpleXMLElement->attributes()
获取对属性的引用。结果:
与将名称空间声明添加为前缀有假名称空间的属性相比,这样做的优点是它被视为名称空间,而不是名称中碰巧具有名称空间前缀的属性。
结果:
SimpleXMLElement
can only add namespaces indirectly. Since you can only add element and attribute nodes, not namespace declarations, text nodes or other node types, you must somehow make use ofSimpleXMLElement->addAttribute()
orSimpleXMLElement->addChild()
. The latter will only add new namespaces to the child, so it's of no use here.addAttribute()
will add the namespace along with an attribute to the given element, so if you then remove the attribute, you're left with the namespace as desired.There isn't an obvious method of removing an attribute, but the use of
unset
as shown in several answers to "Remove a child with a specific attribute, in SimpleXML for PHP" can be adapted to the task, usingSimpleXMLElement->attributes()
to get a reference to the attribute.Result:
The advantage this has over adding the namespace declaration as an attribute with a fake namespace prepended is it's treated as a namespace, rather than an attribute that happens to have something that looks like a namespace prefix in its name.
Result: