【发布时间】:2017-01-26 21:11:48
【问题描述】:
我正在尝试在 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');
对于每种外观,我都会收到以下警告:
- SimpleXMLElement::__construct():命名空间错误:Story 上的命名空间前缀 idPkg 未定义
- SimpleXMLElement::__construct(): u102.xml
- SimpleXMLElement::__construct(): ^ in
【问题讨论】:
-
一般建议:学习如何使用 DOMDocument,SimpleXML 没用(而且没那么简单)。
-
我现在正在研究这个选项。仍然希望像上面的函数那样将插入目标定位在最后一个节点之后。
-
InDesign 标记语言主要依赖于 adobe 特定的命名空间。当您从文件中实例化 xml 对象时,您会丢失大部分额外信息,例如名称空间和可能声明的实体。然后,当您稍后尝试使用前缀注入属性时,您会收到错误,因为编译器甚至不知道该前缀来自何处。由您来决定如何向您的 xml 对象声明和添加名称空间。请注意,写入文件可能会导致相同的问题。
-
我已经设法使用 DOMDocument 方法运行了一些成功的测试,使用 Loic 引用的正确命名空间。目前,我无法将插入定位到最后一个元素之后。我的查询似乎有效 $xpath->query("(//idPkg:Story)[last()]")
标签: php xml simplexml adobe-indesign idml