如何使用 xquery 查找节点并向其添加子项? [英] How to use xquery to find node and addChild to it?

查看:33
本文介绍了如何使用 xquery 查找节点并向其添加子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 xpath/xquery 查询特定的 xml 节点,然后向其导入/添加子节点?

Is it possible to use xpath/xquery to query for a specific xml node, and then import/add a child node to it?

示例(取自 http://codepad.org/gJ1Y2LjM 的代码,在类似问题中提出,但不一样):

Example (code taken from http://codepad.org/gJ1Y2LjM , that was presented in similar question, but not the same):

1.我想添加一个数组

$book = array('isbn'=>123456789099, 'title' => 'Harry Potter 3', 'author' => 'J K. Rowling', 'edition' => '2007');

2.到现有的 XML.

2. To an existing XML.

$doc = new DOMDocument();
$doc->loadXML('<?xml version="1.0"?>
<books>
    <book>
        <isbn>123456789098</isbn>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <edition>2005</edition>
    </book>
    <book>
        <placeItHere></placeItHere>
        <isbn>1</isbn>
        <title>stuffs</title>
        <author>DA</author>
        <edition>2014</edition>
    </book>
</books>');

3.为此,请使用以下片段.

3. To do that it is used the following fragment.

$fragment = $doc->createDocumentFragment();
$fragment->appendXML("    <book>
<isbn>{$book['isbn']}</isbn>
        <title>{$book['title']}</title>
        <author>{$book['author']}</author>
        <edition>{$book['edition']}</edition>
    </book>
");

4.但不是将它附加到根节点,而是我在互联网上找到的几乎所有示例:

4. But instead of appending it to the root node, has almost every example i found on internet:

$doc->documentElement->appendChild($fragment);

5.我想将它 (p.ex) 附加到在/books/book/placeItHere 中找到的节点,而不是使用 getElementbyId 或 tagName,而是使用 xpath/xquery.我试过了

5. I want to append it (p.ex) to the node found in /books/book/placeItHere and not using getElementbyId or tagName, but a xpath/xquery. I tryed

$xp = new domxpath($doc);
$parent = $xp->query("books/book/placeItHere");

到达节点,但从未设法将其用作父节点.

to reach the node, but never managed to use it as the parent.

问题:如何使用该位置来追加子 $fragment?可能吗?

**YOUR BEAUTIFUL MAGIC**

最后我会保存它.

echo $doc->saveXML();

感谢您为我提供的任何帮助.

Thank you for any help you can give to me.

推荐答案

几个问题:

  1. 您的 xpath 表达式应该是 /books/book/placeItHere(以 / 开头).
  2. DOMXPath::query() 返回DOMNodeList 而不是 DOMNode,所以你需要获取你的 item() 来自它.
  1. Your xpath expression should be /books/book/placeItHere (with the leading /).
  2. DOMXPath::query() returns a DOMNodeList rather than a DOMNode, so you'll need to grab your item() from it.

我很少推荐使用文档片段,因为对原始 XML 的快速和松散经常会导致问题.例如,如果您的书名包含一个 & 符号 appendXML() 会导致解析器错误.

I rarely recommend using document fragments as playing fast and loose with raw XML regularly leads to problems. For example if your book title included an ampersand appendXML() would crap out with a parser error.

相反,我建议 createElement()createTextNode(),它将处理诸如& 自动转换为 &amp;.

Instead I suggest createElement() and createTextNode(), which will handle transforming things like & into &amp; automatically.

$xml = <<<'XML'
<?xml version="1.0"?>
<books>
    <book>
        <isbn>123456789098</isbn>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <edition>2005</edition>
    </book>
    <book>
        <placeItHere></placeItHere>
        <isbn>1</isbn>
        <title>stuffs</title>
        <author>DA</author>
        <edition>2014</edition>
    </book>
</books>
XML;

$book = [
    'isbn'    => 123456789099,
    'title'   => 'Harry Potter 3',
    'author'  => 'J K. Rowling',
    'edition' => '2007'
];

$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

$placeItHere = $xpath->query('/books/book/placeItHere')->item(0);
$newBook = $placeItHere->appendChild($dom->createElement('book'));
foreach ($book as $part => $value) {
    $element = $newBook->appendChild($dom->createElement($part));
    $element->appendChild($dom->createTextNode($value));
}

echo $dom->saveXML();

<小时>

输出:

<?xml version="1.0"?>
<books>
  <book>
    <isbn>123456789098</isbn>
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <edition>2005</edition>
  </book>
  <book>
    <placeItHere>
      <book>
        <isbn>123456789099</isbn>
        <title>Harry Potter 3</title>
        <author>J K. Rowling</author>
        <edition>2007</edition>
      </book>
    </placeItHere>
    <isbn>1</isbn>
    <title>stuffs</title>
    <author>DA</author>
    <edition>2014</edition>
  </book>
</books>

这篇关于如何使用 xquery 查找节点并向其添加子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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