简体   繁体   English

使用importNode而不是root或documentElement

[英]Use importNode other than the root or documentElement

Premise: The story is republished and any current tagnames should be preserved and any new tagnames should be added, creating a new story that has both original and new tagnames. 前提:将重新发布该故事,并保留所有当前标记名,并添加任何新标记名,从而创建一个既包含原始标记名又包含新标记名的新故事。

This all works... my issue is that if the republished story doesn't have a tags element at all, I need to create it and then import the tagnames from the original story. 所有这些都有效……我的问题是,如果重新发布的故事根本没有标签元素,则需要创建它,然后从原始故事中导入标签名。

$newXML='<story><metadata></metadata></story>';
$oldXML='<story><metadata><tags><tagname attribute="info"></tagname></tags></metadata></story>';

$newDom = new DomDocument;
$newDom->loadXML($newXML);

$newTagsNode = $newDom->createElement('tags','');

$oTags = $newDom->importNode($newTagsNode, true);
$newDom->documentElement->appendChild($oTags);

The issue is that I don't want oTags to live in 'documentElement' or 'root', I want them to live in story/metadata. 问题是我不希望oTag驻留在“ documentElement”或“ root”中,我希望它们驻留在故事/元数据中。

Is there a way to xpath/query/select 'metadata' and then append? 有没有办法xpath / query /选择“元数据”然后追加?

Use DOMXpath to find a list of elements by XPath. 使用DOMXpath通过XPath查找元素列表。 This will still return a node list, but you can provide more precision in your search. 这仍然会返回节点列表,但是您可以在搜索中提供更高的精度。 If at least one element is returned, make that the target node for the append operation. 如果返回至少一个元素,则将其作为附加操作的目标节点。

$xpath = new DOMXpath($newDom);
$elemList = $xpath->query("/story/metadata");

if ($elemList->length > 0) {
    $elemList->item(0)->appendChild($oTags);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 phpMyAdmin:除root用户外,无法使用其他用户登录 - phpMyAdmin: Can't log in with user other than root php包含根目录以外的目录中的文件 - php include files from directories other than root directory 苗条的骨架不响应除root以外的任何新路由 - Slim skeleton not responding to any new routes other than root 如何使用/ web以外的Web根部署yii2 - How to deploy yii2 with a web root other than /web PHP上载了Web根目录(htdocs)文件夹以外的目录? - PHP Upload dir other than web root (htdocs) folder? 当header.php文件包含在除根文件夹之外的其他文件夹中时,css无法正常工作 - css not working when header.php file included in other than root folder but working on root folder 除了使用$ _SERVER ['DOCUMENT_ROOT']之外,还有更好的方法可以返回目录根目录吗? - is there a better way to go back to the directory root other than using $_SERVER['DOCUMENT_ROOT']? 如何告诉 PHPStorm 调试器,我的项目有项目根以外的文档根 - How tell PHPStorm debugger, that my project has document root other than project root Symfony 3-声明并使用控制器以外的串行器 - Symfony 3 - Declare and use the serializer other than in a controller 如何允许Apache显示除文档根目录以外的其他目录中的内容? - How to allow Apache to display content from other directories than document root?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM