Using XSLT merge 2 XML, which come from PHP transformToXML - php

I am beginner in PHP, XSLT. Found solution for transform XML using XSLT:
$xml = Array2XML::createXML('Document', $result);
$xsl = new DOMDocument;
$xsl->load('Teema.xsl');
$processor = new XSLTProcessor();
$processor->importStyleSheet($xsl);
$results=$processor->transformToXML($xml);
$results=$processor->transformToUri($xml,"NewTeema.xml" );
But, what to do if I have 2 or more XMLs?
This $xml is not file, and I dont want to save each xml, like file on server (because it is was converted response json). Any ideas?

In the XSLT you can load additional document using the document() function.
Another possibility is to register a PHP function that loads the file and returns the value or DOM node.

Good solution, but I decided to merge 2 json, like this:
json_encode(array_merge(json_decode($result,
true),json_decode($resultProducts, true)));
And the use = Array2XML::createXML('Document', $result);

Related

Need to know how to process 1 XML file with 2 XSL files using PHP SimpleXML

I am very new to XSL. I need to process the same XML file with 2 XSL files using PHP SimpleXML. I have tried a few different approaches with no luck.
$xmlfile = 'media/xml_files/article.xml';
if (file_exists($xmlfile)) {
$xml = simplexml_load_file($xmlfile) or die("Error: Cannot create object");}
$xslfile = media/xsl_files/jats-html.xsl;
$xsl = simplexml_load_file($xslfile);
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
I need to process the XML with jats-PMCcit.xsl before it goes through the jats-html.xsl transform. Can somebody please point me in the right direction? I can't seem to find an answer online anywhere.
You can try using XSLTProcessor::transformToDoc() to process the first transformation and get intermediate transformation result in a DOMDocument object type. Then you can pass the DOMDocument object to transformToXML() to get the final transformation result.

XML tag related questions

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
print $xmlDoc->saveXML();
?>
In the above code sample I don't understand the last line. why we use saveXML(). Is there anyway to print the xml file without using saveXML(). Please anyone can answer my questions.
If you are loading the xml from a file and you are not modifying it in any way, there's no need to call saveXML(). saveXML is mostly used when you create an xml document from scratch.
If you only want to check the contents of the file, you could use file_get_contents, instead of using DomDocument. DomDocument is more useful when you want to make changes or parse.

How can I load multiple XML documents inside my PHP, DOM script?

I have two different XML structured documents, an XSLT that renames the elements and nodes of them to satisfy both and a PHP code that will save them into a new XML doc.
This is the code I used for testing purposes, however how can I load two or more paths like book1.xml and book2.xml into the $xml ? I know how to $dom->load( 'book1.xml' );
<?php
// create an XSLT processor and load the stylesheet as a DOM
$xproc = new XsltProcessor();
$xslt = new DomDocument;
$xslt->load('stylesheet.xslt'); // this contains the code from above
$xproc->importStylesheet($xslt);
// your DOM or the source XML (copied from your question)
$xml = '';
$dom = new DomDocument;
$dom->loadXML($xml);
?>
Read about and use the standard XSLT function document().
In XSLT 2.0 there is also standard support for producing multiple result documents -- read about the <xsl:result-document> element.
If you are bound to XSLT 1.0, you cannot produce more than one result document in one transformation. You can either use extension libraries (EXSLT, the <exsl:document> extension element) or you can produce all results in one result document and then produce every single result out of it using another transformation, that you run once for producing each result.

Create new XML file and write data to it?

I need to create a new XML file and write that to my server. So, I am looking for the best way to create a new XML file, write some base nodes to it, save it. Then open it again and write more data.
I have been using file_put_contents() to save the file. But, to create a new one and write some base nodes I am not sure of the best method.
Ideas?
DOMDocument is a great choice. It's a module specifically designed for creating and manipulating XML documents. You can create a document from scratch, or open existing documents (or strings) and navigate and modify their structures.
$xml = new DOMDocument();
$xml_album = $xml->createElement("Album");
$xml_track = $xml->createElement("Track");
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
$xml->save("/tmp/test.xml");
To re-open and write:
$xml = new DOMDocument();
$xml->load('/tmp/test.xml');
$nodes = $xml->getElementsByTagName('Album') ;
if ($nodes->length > 0) {
//insert some stuff using appendChild()
}
//re-save
$xml->save("/tmp/test.xml");
PHP has several libraries for XML Manipulation.
The Document Object Model (DOM) approach (which is a W3C standard and should be familiar if you've used it in other environments such as a Web Browser or Java, etc). Allows you to create documents as follows
<?php
$doc = new DOMDocument( );
$ele = $doc->createElement( 'Root' );
$ele->nodeValue = 'Hello XML World';
$doc->appendChild( $ele );
$doc->save('MyXmlFile.xml');
?>
Even if you haven't come across the DOM before, it's worth investing some time in it as the model is used in many languages/environments.
With FluidXML you can generate and store an XML document very easily.
$doc = fluidxml();
$doc->add('Album', true)
->add('Track', 'Track Title');
$doc->save('album.xml');
Loading a document from a file is equally simple.
$doc = fluidify('album.xml');
$doc->query('//Track')
->attr('id', 123);
https://github.com/servo-php/fluidxml

Create and Save XML file to Server Using PHP

I'm using PHP to extract data from a MySQL database. I am able to build an XML file using DOM functions. Then using echo $dom->saveXML(); , I am able to return the XML from an AJAX call. Instead of using AJAX to get the XML, how would I save the XML file to a spot on the server? Thanks
Use the DOMDocument::save() method to save the XML document into a file:
$dom->save('document.xml');
Doesn't DOMDocument::save() help you?
Use PHP XML DOM Parser to create and save XML file. The following code and tutorial would be found from here - Create and Save XML File using PHP
$xmlString = 'Insert XML Content';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('fileName.xml');
Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:
$strxml = $dom->saveXML();
$handle = fopen("yourxmlfile.xml", "w");
fwrite($handle, $strxml);
fclose($handle);
And you are done.
Remember that the user running your application server (Apache, probably) will need permissions to write in the directory you are placing the XML file.

Categories