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.
Related
I have the following XML file:-
http://services.jupix.co.uk/api/get_properties.php?clientID=553a6644a7dc21df315c90e0140ad60d&passphrase=324dd464aa7ba96ad0e8ab6d8f423162
What I need to do is fetch this data and split it into two different PHP files.
Within <property> you will see:-
<department>Sales</department>
and
<department>Lettings</department>
These are the fields I need it splitting by.
Any idea how I can do this?
With DOMDocument and XPath:
$dom = new DOMDocument;
$dom->load($url);
$xpath = new DOMXPath($dom);
$properties = $xpath->evaluate('/properties/property[not(department="Sales")]');
foreach ($properties as $property) {
$property->parentNode->removeChild($property);
}
echo $dom->save('Sales.xml');
This will load the XML from the remote URL into a DOMDocument and remove all property items from properties that do not contain an element where department is "Sales". Then it saves the resulting document to the file sales.xml.
For Lettings, you just adapt the XPath and save file to say Lettings instead of Sales.
You can also do this with SimpleXml or XmlReader+XmlWriter or an XSL transformation easily. I will leave that for others to post though.
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);
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.
I am trying to load a xml document I created using PHP and DOM into a xslt sheet, but having no luck.
$xml_string = $doc->saveXML();
//echo $xml_string;
$xml = new DOMDocument;
$xml->load($xml_string);
$xsl = new DOMDocument;
$xsl->load('musicInformation.xslt');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
I created a xml file based off some data extracted from a database and instead of saving it as an actual document I saved it as a string, I put the string into the xslt sheet and this error occurred
Warning: I/O warning : failed to load
external entity Warning: xpath.c:11079
Internal error: document without root
in
/home/dd615/public_html/webservice.php
on line 73
Any help would be much appreciated.
A string is not XML.
Valid XML needs a root element (that is, a single element that wraps all other elements in the document, apart from the XML declaration).
Such as this:
<?xml version="1.0" ?>
<root>
<element></element>
<element></element>
...
</root>
If you have multiple such roots, the XML is not valid and will fail to load.
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