PHP transform xml string to XSLT - php

I'm trying to run an xslt script over a xml response. But I'm having problems getting PHP/XSLT to understand the response. My PHP looks like this, the $data variable contains the XML
$movies = new SimpleXMLElement($data);
// Load the XML source
$xml = new DOMDocument;
$xml->load($movies);
$xsl = new DOMDocument;
$xsl->load('/var/www/html/app/views/xslt/rdf.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
$res = $proc->transformToXML($xml);
I'm getting
production.ERROR: exception 'ErrorException' with message 'DOMDocument::load(): I/O warning : failed to load external entity "/var/www/public/
"'
as my error. I've tried using simplexml_load_file and simplexml_load_string but have similar results.

Related

Converting PHP to ERB in Ruby on Rails

I'm new to Ruby on Rails and I have some code in PHP that will transform my xml document with my xsl document on my browser.
I need some up help converting the code to erb.
<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('famousQoutes.xml');
// Load XSL file
$xsl = new DOMDocument;
$xsl->load('famousQ.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
// Attach the xsl rules
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
Any help is much appreciated, thanks in advance.

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.

error in loading url in simplexml_load_file

I have an rss feed which i would like to read from in my page, i have a local copy of the feed which reads fine but i am required to use the online version. From what i can see i am doing this correctly:
$url ='http//www.numyspace.co.uk/~cgel1/holidays/holidays.xml';
$holidayDoc = simplexml_load_file($url);
However i am met with the following error:
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity
"http//www.numyspace.co.uk/~cgel1/holidays/holidays.xml"
Why is this not working?
$use_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file("http://www.numyspace.co.uk/~cgel1/holidays/holidays.xml");
if (!$xml) {
//throw new Exception("Cannot load xml source.\n");
}
libxml_clear_errors();
libxml_use_internal_errors($use_errors);
You missed a colon

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.

loading XML string into Xslt sheet

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.

Categories