PHP 更改 XML 文件中的 xmlns [英] PHP change xmlns in XML file

查看:27
本文介绍了PHP 更改 XML 文件中的 xmlns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构的xml:

I have xml with the following structure:

<?xml version="1.0"?>
<ONIXMessage xmlns="http://test.com/test">
 ...data...
</ONIXMessage>

我需要使用我自己的值更改 xmlns 属性.我该怎么做?最好使用 DOMDocument 类.

I need to change xmlns attribute with my own value. How can I do it? Preferably with DOMDocument class.

推荐答案

我需要使用我自己的值更改 xmlns 属性.我该怎么做?最好使用 DOMDocument 类.

这是不可能的.每个 DOMDocument 都有一个根/文档元素.

This by design is not possible. Every DOMDocument has a single root/document element.

在您的示例 XML 中,根元素是:

In your example XML that root element is:

{http://test.com/test}ONIXMessage

我将元素名称写为 expanded-name ,约定将命名空间 URI 放在前面,并用尖括号括起来.

I write the element name as an expanded-name with the convention to put the namespace URI in front enclosed in angle brackets.

以显示其完整的形式编写元素名称 expanded-name 还表明您不仅要更改此处的属性值,而且要更改其命名空间 URI一个特定的元素.所以你想改变元素名称.如果子元素在同一命名空间中,也可能还有它包含的任何子元素名称.

Writing the element name in a form that shows it's entire expanded-name also demonstrates that you do not only want to change the value of an attribute here, but you want to change the namespace URI of a specific element. So you want to change the element name. And probably also any child element name it contains if the child is in the same namespace.

作为 xmlns 属性 仅反映元素本身的命名空间 URI,您无法更改它.一旦在 DOMDocument 中设置,您就无法更改它.

As the xmlns attribute only reflects the namespace URI of the element itself, you can not change it. Once it is set in DOMDocument, you can not change it.

你可以替换整个元素,但子元素的命名空间也不会改变.这是一个与您的 XML 类似的示例,其中只有 textnode 子节点(未命名空间):

You can replace the whole element, but the namespace of the children is not changed either then. Here an example with an XML similar to yours with only textnode children (which aren't namespaced):

$xml = <<<EOD
<?xml version="1.0"?>
<ONIXMessage xmlns="uri:old">
 ...data...
</ONIXMessage>
EOD;

$doc = new DOMDocument();
$doc->loadXML($xml);
$newNode = $doc->createElementNS('uri:new', $doc->documentElement->tagName);
$oldNode = $doc->replaceChild($newNode, $doc->documentElement);

foreach(iterator_to_array($oldNode->childNodes, true) as $child) {
    $doc->documentElement->appendChild($child);
}

结果 XML 输出为:

Resulting XML output is:

<?xml version="1.0"?>
<ONIXMessage xmlns="uri:new">
 ...data...
</ONIXMessage>

现在将输入 XML 更改为包含子项的内容,例如

Changing the input XML now to something that contains children like

<?xml version="1.0"?>
<ONIXMessage xmlns="uri:old">
   <data>
     ...data...
   </data>
</ONIXMessage>

然后将创建以下输出,注意现在再次弹出的旧命名空间 URI:

Will then create the following output, take note of the old namespace URI that pops up now again:

<?xml version="1.0"?>
<ONIXMessage xmlns="uri:new">
   <default:data xmlns:default="uri:old">
     ...data...
   </default:data>
</ONIXMessage>

如您所见,DOMDocument 不提供开箱即用地替换现有元素的命名空间 URI 的功能.但希望到目前为止,通过此答案中提供的信息,可以更清楚地说明为什么如果该属性值已经存在,则无法更改该值.

As you can see DOMDocument does not provide a functionality to replace namespace URIs for existing elements out of the box. But hopefully with the information provided in this answer so far it is more clear why exactly it is not possible to change that attributes value if it already exists.

基于 libxml 的 PHP 扩展中基于 expat 的解析器 确实允许更改"现有属性值,无论它是否是 xmlns* 属性 - 因为它只是解析数据,您可以使用它即时处理它.

The expat based parser in the libxml based PHP extension does allow to "change" existing attribute values regardless if it is an xmlns* attribute or not - because it just parses the data and you can process it on the fly with it.

一个工作示例是:

$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<ONIXMessage xmlns="uri:old">
  <data>
    ...data...
  </data>
</ONIXMessage>
EOD;

$uriReplace = [
    'uri:old' => 'uri:new',
];

$parser = xml_parser_create('UTF-8');
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_default_handler($parser, function ($parser, $data) {
    echo $data;
});
xml_set_element_handler($parser, function ($parser, $name, $attribs) use ($xml, $uriReplace) {
    $selfClosing = '/>' === substr($xml, xml_get_current_byte_index($parser), 2);
    echo '<', $name;
    foreach ($attribs as $name => $value) {
        if (substr($name, 0, 5) === 'xmlns' && isset($uriReplace[$value])) {
            $value = $uriReplace[$value];
        }
        printf(' %s="%s"', $name, htmlspecialchars($value, ENT_COMPAT | ENT_XML1));
    }
    echo $selfClosing ? '/>' : '>';
}, function ($parser, $name) use ($xml) {
    $selfClosing = '/>' === substr($xml, xml_get_current_byte_index($parser) - 2, 2);
    if ($selfClosing) return;
    echo '</', $name, '>';
});

xml_parse($parser, $xml, true);
xml_parser_free($parser);

然后输出透明地将命名空间 URI 从 uri:old 更改为 uri:new:

The output then has transparently changed the namespace URI from uri:old to uri:new:

<ONIXMessage xmlns="uri:new">
   <data>
     ...data...
   </data>
</ONIXMessage>

如本示例所示,您在 XML 中使用的每个 XML 功能都需要使用解析器进行处理.例如,缺少 XML 声明.但是,可以通过实现缺少的处理程序类回(例如 对于 CDATA 部分)或通过输出缺少的输出(例如对于缺少"XML 声明).我希望这会有所帮助,并向您展示如何更改这些不打算更改的值的替代方法.

As this example shows, each XML feature you make use of in your XML needs to be handled with the parser. For example the XML declaration is missing. However these can be added by implementing missing handler classbacks (e.g. for CDATA sections) or by outputting missing output (e.g. for the "missing" XML declaration). I hope this is helpful and shows you an alternative way on how to change even these values that are not intended to change.

这篇关于PHP 更改 XML 文件中的 xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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