Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, you can achieve this with PHP using DOM.</p> <p>Given an XML format in a string like this:</p> <pre><code> $xml = &lt;&lt;&lt;EOX &lt;root&gt; &lt;eerste_kwartier&gt;naam leeg&lt;/eerste_kwartier&gt; &lt;eerste_kwartier&gt;leeg leeg&lt;/eerste_kwartier&gt; &lt;/root&gt; EOX; </code></pre> <p>you could do what follows:</p> <h2>1 - Use PHP and DOM</h2> <p>Using DOM you can obtain a list of elements having the same tag name with the getElementsByTagName() method, walking that list and applying text transformations you prefer.</p> <p>Example:</p> <pre><code>// Load source XML $xmlObj = new DOMDocument("1.0", "UTF-8"); $xmlObj-&gt;loadXML($xml); // Obtain interesting nodes into a node list object $nodeList = $xmlObj-&gt;getElementsByTagName('eerste_kwartier'); // Prepare destination XML object $xmlObjResult = new DOMDocument("1.0", "UTF-8"); $rootElement = $xmlObjResult-&gt;createElement("root"); // Walk node list for ($i = 0; $i &lt; $nodeList-&gt;length; $i++) { $tmp = $nodeList-&gt;item($i)-&gt;nodeValue; // Use regular expressions for greater flexibility in selecting text to replace $tmp = preg_replace('/naam/', 'leeg', $tmp); // Prepare destination element $eerste_kwartier = $xmlObjResult-&gt;createElement("eerste_kwartier"); $cdata = $xmlObjResult-&gt;createCDATASection($tmp); $eerste_kwartier-&gt;appendChild($cdata); $rootElement-&gt;appendChild($eerste_kwartier); } // Append root element to the document $xmlObjResult-&gt;appendChild($rootElement); // Enjoy your XML echo $xmlObjResult-&gt;saveXML(); </code></pre> <h2>2 - Use PHP and DOMXPath</h2> <p>The example above could be easily changed to use XPath's query() instead of DOM's getElementsByTagName().</p> <p>So you would do:</p> <pre><code>// Load source XML $xmlObj = new DOMDocument("1.0", "UTF-8"); $xmlObj-&gt;loadXML($xml); // Obtain interesting nodes into a node list object $xpath = new DOMXPath($xmlObj); $nodeList = $xpath-&gt;query("//eerste_kwartier"); // Prepare destination XML object $xmlObjResult = new DOMDocument("1.0", "UTF-8"); $rootElement = $xmlObjResult-&gt;createElement("root"); // Walk node list for ($i = 0; $i &lt; $nodeList-&gt;length; $i++) { $tmp = $nodeList-&gt;item($i)-&gt;nodeValue; // Use regular expressions for greater flexibility in selecting text to replace $tmp = preg_replace('/naam/', 'leeg', $tmp); // Prepare destination element $eerste_kwartier = $xmlObjResult-&gt;createElement("eerste_kwartier"); $cdata = $xmlObjResult-&gt;createCDATASection($tmp); $eerste_kwartier-&gt;appendChild($cdata); $rootElement-&gt;appendChild($eerste_kwartier); } // Append root element to the document $xmlObjResult-&gt;appendChild($rootElement); // Enjoy your XML echo $xmlObjResult-&gt;saveXML(); </code></pre> <h2>Conclusions</h2> <p>As you've certainly noticed the two options are quite similar.</p> <p>If your search within the document resolves into searching for the same tag name I would suggest going with option 1; if your search should be even slighly more articulated than that, I suggest option 2, as XPath has loads of nice weapons to fight XML search-through complexity (a brief introduction to XPath can be found <a href="http://www.w3schools.com/xpath/default.asp" rel="nofollow">here</a>, but there's a lot of material available just googling around).</p> <p>I wouldn't get XSL into the picture, unless you should apply a higher scale and articulated transformation of your content, but just in case: <a href="http://htmlite.com/XML008.php" rel="nofollow">http://htmlite.com/XML008.php</a> (an extremely brief XSLT introduction tutorial, which should tickle your appetite).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload