The XML path language ( Xpath
) was created for facilitate the search for elements within an XML tree :
The primary purpose of XPath is to address parts of an XML document
The main goal of XPath is to access parts of a document
XML
So it is not advisable to use a path xpath
as a description of the structure of a document XML
. That's why we use the DTD .
If you want to generate a% basic% co from a basic path XML
you can use this small piece of code:
<pre><?php
function agregar_xml_desde_xpath($xpath, $dom) {
$elementos = explode('/', $xpath);
$actual = $dom;
foreach ($elementos as $elemento) {
if (!empty($elemento)) {
$nuevo = $dom->createElement($elemento);
$actual->appendChild($nuevo);
$actual = $nuevo;
}
}
}
$dom = new DOMDocument();
agregar_xml_desde_xpath('//nodo1/nodo_1_hijo', $dom);
echo htmlspecialchars($dom->saveXML(null, LIBXML_NOEMPTYTAG));
?><pre>
The result obtained is:
<?xml version="1.0"?>
<nodo1><nodo_1_hijo></nodo_1_hijo></nodo1>
Keep in mind that it is a very simple implementation that only the node tree obtains. For more complex routes (using attributes, functions, etc.) the implementation could become complex.