Take this very basic XML document (this is just a basic example)
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>first title<sep/>first author</book>
<book>second title<sep/>second author</book>
<book>third title<sep/>third author</book>
<book><sep/>fourth author</book>
</books>
The problem is pretty simple... How do I insert the title of the fourth book ?
And how do I automatically detect that the title is not set for the fourth book ?
What I'd really need is the php DOMDocument to tell me that there is an empty text node in front of the last <sep/>
.
But with DOMDocument, the node simply does not exist:
<?php
$path = './books.xml';
$dom = new DOMDocument();
$dom->load($path);
$books = $dom->getElementsByTagName('book');
foreach ($books as $index => $book) {
echo $book->childNodes->length . ' children' . PHP_EOL;
}
The code above shows only 2 childnodes for the last book
node.
And using Xpath /books/book[4]/text()[1]
points to the "author" text node of the fourth book and does not point to the empty text node in front of the <sep/>
Let me know if this is not perfectly clear...
Thanks in advance for your help !