首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DOMDocument生成错误元素上的命名空间声明。

DOMDocument生成错误元素上的命名空间声明。
EN

Stack Overflow用户
提问于 2014-09-18 11:39:10
回答 1查看 63关注 0票数 0

下面是我从http://pastebin.com/7FBysx2X使用的PHP代码

代码语言:javascript
复制
$doc = new DOMDocument('1.0', 'UTF-8');
$xns = 'http://www.w3.org/2000/xmlns/';
$mns = 'http://example.com/aBc/2/';
$ons = 'http://example.com/test/2005/something';
$ns =  'http://example.com/main/';

$firstChild = $doc->createElement('firstChild');
$firstChild->setAttributeNS($xns, 'xmlns:cns1', $mns);
$firstChild->setAttributeNS($xns, 'xmlns:i', $ons);

$elements = $doc->createElementNS($mns, 'cns1:elements');

for($i = 0; $i < 3; $i++) {
    $e = $doc->createElementNS($mns, 'cns1:element');
    for($k = 0; $k < 2; $k++) {
        $r = rand(100, 999);
        $value = round(($r*rand(1,9))/rand(1,9), 2);
        $ce = $doc->createElementNS($mns, "cns1:elementValue$r", $value);
        $e->appendChild($ce);
    }
    $elements->appendChild($e);
}
$firstChild->appendChild($elements);

$otherTag = $doc->createElementNS($mns, 'cns1:otherTag', 'some_value');
$emptyTag = $doc->createElementNS($mns, 'cns1:emptyTag');
$emptyTag->setAttributeNS($ons, 'i:nil', 'true');

$firstChild->appendChild($otherTag);
$firstChild->appendChild($emptyTag);

$main = $doc->createElementNS($ns, 'main');
$main->appendChild($firstChild);

$doc->appendChild($main);


header('Content-Type: text/xml');

echo $doc->saveXML();

上面的代码生成这样的XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<main xmlns:cns1="http://example.com/aBc/2/" xmlns:i="http://example.com/test/2005/something" xmlns="http://example.com/main/">
  <firstChild xmlns:cns1="http://example.com/aBc/2/" xmlns:i="http://example.com/test/2005/something">
    <cns1:elements>
      <cns1:element>
        <cns1:elementValue303>101</cns1:elementValue303>
        <cns1:elementValue608>304</cns1:elementValue608>
      </cns1:element>
      <cns1:element>
        <cns1:elementValue735>147</cns1:elementValue735>
        <cns1:elementValue901>4505</cns1:elementValue901>
      </cns1:element>
    </cns1:elements>
    <cns1:otherTag>some_value</cns1:otherTag>
    <cns1:emptyTag i:nil="true"/>
  </firstChild>
</main>

文档应该如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<main xmlns="http://example.com/main/">
  <firstChild xmlns:cns1="http://example.com/aBc/2/" xmlns:i="http://example.com/test/2005/something">
    <cns1:elements>
      <cns1:element>
        <cns1:elementValue303>101</cns1:elementValue303>
        <cns1:elementValue608>304</cns1:elementValue608>
      </cns1:element>
      <cns1:element>
        <cns1:elementValue735>147</cns1:elementValue735>
        <cns1:elementValue901>4505</cns1:elementValue901>
      </cns1:element>
    </cns1:elements>
    <cns1:otherTag>some_value</cns1:otherTag>
    <cns1:emptyTag i:nil="true"/>
  </firstChild>
</main>

问题就在<main>标签上。为什么它有cns1i命名空间声明?它们应该只在firstChild元素中。我需要改变什么才能得到所需的结构?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-18 19:20:20

这是由于将子节点添加到尚未添加到文档中的节点造成的。

将代码更改为:

代码语言:javascript
复制
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$xns = 'http://www.w3.org/2000/xmlns/';
$mns = 'http://example.com/aBc/2/';
$ons = 'http://example.com/test/2005/something';
$ns =  'http://example.com/main/';

$main = $doc->createElementNS($ns, 'main');
$doc->appendChild($main);

$firstChild = $doc->createElement('firstChild');
$firstChild->setAttributeNS($xns, 'xmlns:cns1', $mns);
$firstChild->setAttributeNS($xns, 'xmlns:i', $ons);

$doc->getElementsByTagName('main')->item(0)->appendChild($firstChild);

$elements = $doc->createElementNS($mns, 'cns1:elements');

$doc->getElementsByTagName('firstChild')->item(0)->appendChild($elements);

for($i = 0; $i < 3; $i++) {
    $e = $doc->createElementNS($mns, 'cns1:element');
    $doc->getElementsByTagName('elements')->item(0)->appendChild($e);
    for($k = 0; $k < 2; $k++) {
        $r = rand(100, 999);
        $value = round(($r*rand(1,9))/rand(1,9), 2);
        $ce = $doc->createElementNS($mns, "cns1:elementValue$r", $value);
        $doc->getElementsByTagName('element')->item($i)->appendChild($ce);
    }
}

$otherTag = $doc->createElementNS($mns, 'cns1:otherTag', 'some_value');
$emptyTag = $doc->createElementNS($mns, 'cns1:emptyTag');
$emptyTag->setAttributeNS($ons, 'i:nil', 'true');

$doc->getElementsByTagName('firstChild')->item(0)->appendChild($otherTag);
$doc->getElementsByTagName('firstChild')->item(0)->appendChild($emptyTag);


echo $doc->saveXML();

生成与您期望的XML完全相同的XML。也许有更多的‘漂亮’或‘适当’的方式来做这件事,但这一个肯定是有效的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25911676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档