SimpleXML 具有 xmlns:xmlns="" 声明- 无法删除

发布于 2024-11-19 06:29:47 字数 724 浏览 4 评论 0 原文

太气人了,简直说不出话来。我已经用 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>...

我不理解所有这些名称空间声明。这是怎么回事?

So infuriating, I can hardly talk. I've assembled an RSS feed with SimpleXML and yet - it's using name spaces, which are right now. But, it's constantly trying to declare xmlns:xmlns="" in the root node, when output. Even though I do no such thing.

It starts with

$rssXML->addAttribute("version", '2.0');
$rssXML->addAttribute("xmlns:media", "http://search.yahoo.com/mrss/", '');
$rssXML->addAttribute("xmlns:dcterms", "http://purl.org/dc/terms/", '');

and after this I do:-

header("Content-Type: application/rss+xml");

echo $syndicationXML->asXML();

Yet it outputs :-

<?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>...

I do not understand all this namespace declaration. What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香草可樂 2024-11-26 06:29:47

SimpleXML 的问题在于它的 addAttribute 函数添加了一个属性,而不是一个命名空间,虽然它看起来像您想要的那样,但它并不意味着按照您使用它的方式使用。

它的目的是添加一个属于特定命名空间(指定为第三个参数)的值,而不是添加命名空间本身。之所以最终得到 xmlns:xmlns 是因为 SimpleXML 发现您在指定名称 xmlns:media 时使用了 xmlns 命名空间因此它创建了一个空的 xmlns:xmlns

以下是您问题的 2 个解决方案:

1。在构造函数中的命名空间中指定。

$rssXML = new SimpleXMLElement('<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/" />');
$rssXML->addAttribute('version', '2.0');

2.使用 preg_replace 替换 xmlns:xmlns=""

echo preg_replace('/xmlns:xmlns=""\s?/', '', $rssXML->asXML());

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 the xmlns namespace when specifying the name xmlns:media for instance so it created an empty xmlns:xmlns.

Here are 2 solutions to your problem:

1. Specify in the namespaces in the constructor.

$rssXML = new SimpleXMLElement('<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/" />');
$rssXML->addAttribute('version', '2.0');

2. Replace xmlns:xmlns="" using preg_replace

echo preg_replace('/xmlns:xmlns=""\s?/', '', $rssXML->asXML());
行至春深 2024-11-26 06:29:47

您写道您想删除它。最好的办法就是不要把它放在第一位。

这通过使用 xmlns: 前缀(另一次)来实现 - 它以某种方式在 simplexml 中启用特殊的操作模式 - 并且提供一个空的命名空间-URI(否则你实际上会问添加这一点):

$rssXML = new SimpleXMLElement('<rss/>');
$rssXML->addAttribute("version", '2.0');
$rssXML->addAttribute("xmlns:xmlns:media", "http://search.yahoo.com/mrss/");
$rssXML->addAttribute("xmlns:xmlns:dcterms", "http://purl.org/dc/terms/");
$rssXML->asXML('php://output');

这将创建以下输出(为了您的阅读乐趣而美化):

<?xml version="1.0"?>
<rss version="2.0" 
     xmlns:media="http://search.yahoo.com/mrss/" 
     xmlns:dcterms="http://purl.org/dc/terms/"/>

这个“作弊”最初在 “无法使用 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):

$rssXML = new SimpleXMLElement('<rss/>');
$rssXML->addAttribute("version", '2.0');
$rssXML->addAttribute("xmlns:xmlns:media", "http://search.yahoo.com/mrss/");
$rssXML->addAttribute("xmlns:xmlns:dcterms", "http://purl.org/dc/terms/");
$rssXML->asXML('php://output');

This creates the following output (beautified for your reading pleasure):

<?xml version="1.0"?>
<rss version="2.0" 
     xmlns:media="http://search.yahoo.com/mrss/" 
     xmlns:dcterms="http://purl.org/dc/terms/"/>

This "cheat" was originally hinted in "Unable to add Attribute with Namespace Prefix using PHP Simplexml" but was missing here.

岁月无声 2024-11-26 06:29:47

SimpleXMLElement 只能间接添加命名空间。由于您只能添加元素和属性节点,而不能添加名称空间声明、文本节点或其他节点类型,因此您必须以某种方式使用 SimpleXMLElement->addAttribute()SimpleXMLElement->addChild()。后者只会向子级添加新的命名空间,因此这里没有用。 addAttribute() 会将命名空间和属性添加到给定元素,因此,如果您随后删除该属性,则会留下所需的命名空间。

没有明显的方法来删除属性,但可以使用 unset ,如“在 SimpleXML for PHP 中删除具有特定属性的子项" 可以使用 SimpleXMLElement->attributes() 获取对属性的引用。

<?php
$rssXML = new SimpleXMLElement('<rss/>');
$rssXML->addAttribute("version", '2.0');
# add a dummy attribute to get the namespace
$rssXML->addAttribute("media:_", '', "http://search.yahoo.com/mrss/");
unset($rssXML->attributes('media', TRUE)[0]);
# and again
$rssXML->addAttribute("dcterms:_", '', "http://purl.org/dc/terms/");
unset($rssXML->attributes('dcterms', TRUE)[0]);

echo $rssXML->asXML(), "\n";

结果:

<?xml version="1.0"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/" version="2.0"/>

与将名称空间声明添加为前缀有假名称空间的属性相比,这样做的优点是它被视为名称空间,而不是名称中碰巧具有名称空间前缀的属性。

var_export($rssXML->getDocNamespaces());

结果:

array (
  'media' => 'http://search.yahoo.com/mrss/',
  'dcterms' => 'http://purl.org/dc/terms/',
)

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 of SimpleXMLElement->addAttribute() or SimpleXMLElement->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, using SimpleXMLElement->attributes() to get a reference to the attribute.

<?php
$rssXML = new SimpleXMLElement('<rss/>');
$rssXML->addAttribute("version", '2.0');
# add a dummy attribute to get the namespace
$rssXML->addAttribute("media:_", '', "http://search.yahoo.com/mrss/");
unset($rssXML->attributes('media', TRUE)[0]);
# and again
$rssXML->addAttribute("dcterms:_", '', "http://purl.org/dc/terms/");
unset($rssXML->attributes('dcterms', TRUE)[0]);

echo $rssXML->asXML(), "\n";

Result:

<?xml version="1.0"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/" version="2.0"/>

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.

var_export($rssXML->getDocNamespaces());

Result:

array (
  'media' => 'http://search.yahoo.com/mrss/',
  'dcterms' => 'http://purl.org/dc/terms/',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文