SimpleXMLElement错误。尝试在特定点之后添加XML

时间:2017-01-26 21:11:49

标签: php xml simplexml adobe-indesign idml

我正在尝试在designmap.xml

之后添加一个新节点

新节点包含一个由下面的数组自定义的src属性。

$newStories = Array ( [0] => u102 [1] => u103 [2] => u107 [3] => u156  );

$designMap = simplexml_load_file('designmap.xml');

foreach ($newStories as $story) {
  $newStoryNode = '<idPkg:Story src="Stories/Story_' . $story . '.xml" />';
  $insert = new SimpleXMLElement($newStoryNode);
  $target = current($designMap->xpath('//idPkg:Story[last()]'));

  simplexml_insert_after($insert, $target);
}

function simplexml_insert_after(SimpleXMLElement $insert, SimpleXMLElement $target)
{
    $target_dom = dom_import_simplexml($target);
    $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
    if ($target_dom->nextSibling) {
        return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
    } else {
        return $target_dom->parentNode->appendChild($insert_dom);
    }
}

$designMap->asXML('designmap.xml');

我为每个外观获得以下警告

  1. SimpleXMLElement :: __ construct():namespace error:未定义Story上的命名空间前缀idPkg
  2. SimpleXMLElement :: __ construct():u102.xml
  3. SimpleXMLElement :: __ construct():^ in

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$xpath->query("//idPkg:Story[last()]")
&#13;
&#13;
&#13;

不确定你是否需要接受第一个声明&#34; // idPkg:Story&#34;