Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use any language that allows you to manipulate xml directly. I suggest finding something with DOM rather than SAX. If you use SAX you have to basically traverse the xml yourself - a pita in my experience. DOM allows you to act on the xml in a more OOP manner.</p> <p>Something that springs immediately to mind would be a wrapper xml around your xml "documents".</p> <p>So something like:</p> <pre><code>&lt;documents&gt; &lt;document&gt; &lt;!-- Your xml here --&gt; &lt;/document&gt; &lt;document&gt; &lt;!-- Your xml here --&gt; &lt;/document&gt; &lt;document&gt; &lt;!-- Your xml here --&gt; &lt;/document&gt; &lt;/documents&gt; </code></pre> <p>The pseudo code would be: Create a document root. Add an element called documents, use that as the root. Iterate each of your xml files. For each file create a new element called document. Add that element to the parent. Load the xml from the file. Import that node into the outer document. Append the imported node into the document elements child collection.</p> <p><strong>EDIT</strong> As promised here is the updated code that was tested and I know works:</p> <pre><code>&lt;?php // Replace the strings below with the actual filenames, add or decrease as fit $filenames = array(0 =&gt; "test.xml", 1 =&gt; "test2.xml", 2 =&gt; "test3.xml" ); $docList = new DOMDocument(); $root = $docList-&gt;createElement('documents'); $docList-&gt;appendChild($root); foreach($filenames as $filename) { $doc = new DOMDocument(); $doc-&gt;load($filename); $xmlString = $doc-&gt;saveXML($doc-&gt;documentElement); $xpath = new DOMXPath($doc); $query = "//product"; // this is the name of the ROOT element $nodelist = $xpath-&gt;evaluate($query, $doc-&gt;documentElement); if( $nodelist-&gt;length &gt; 0 ) { $node = $docList-&gt;importNode($nodelist-&gt;item(0), true); $xmldownload = $docList-&gt;createElement('document'); $xmldownload-&gt;setAttribute("filename", $filename); $xmldownload-&gt;appendChild($node); $root-&gt;appendChild($xmldownload); } } echo $docList-&gt;saveXML(); ?&gt; </code></pre>
 

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