The *
only selects element nodes, not text nodes. So only the <formula>
elements are selected. You need to use node()
. But you could use xpath directly to selected the needed nodes. Look for an explanation of the Kayessian method.
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$nodes = $xpath->evaluate(
'//error[@n="\\Author"][1]
/following-sibling::node()
[
count(
.|
//error[@n="\\Author"][1]
/following-sibling::error[@n="\\address"][1]
/preceding-sibling::node()
)
=
count(
//error[@n="\\Author"][1]
/following-sibling::error[@n="\\address"][1]
/preceding-sibling::node()
)
]'
);
$result = '';
foreach ($nodes as $node) {
$result .= $node->nodeValue;
}
var_dump($result);
Demo: https://eval.in/125494
If you want to save not only the text content, but the XML fragment, you can use DOMDocument::saveXml() with the node as argument.
$result = '';
foreach ($nodes as $node) {
$result .= $node->ownerDocument->saveXml($node);
}
var_dump($result);