PHP no response when XML validation with XSD - php

I need to validate some xml-files with xsd files in a php application. The problem I'm having is when I use domdocument or xmlreader, I keep getting a blank page (ERR_EMPTY_RESPONSE) when executing schema validation. I get no errors at all.
$reader = new XMLReader();
$reader->open("../xml/testxml.xml");
echo $reader->setSchema("../xml/validation.xsd") ? 'valid' : 'invalid';
$doc = new DOMDocument();
$doc->load("../xml/testxml.xml");
echo $doc->schemaValidate("../xml/validation.xsd") ? 'valid' : 'invalid';
Both files & paths of the xml/xsd exists. The php version is 5.3.1 & libxml2 version is 2.7.3 and I'm working in an OSX environment.
UPDATE: WORKAROUND
I have found a way to get it working. Instead of using load($path) & schemaValidate($path), I have used loadXml($string) & schemaValidateSource($string). So instead of giving the file path in the load function I load the content manually via file_get_contents() and give it to the loadXml function. Same for the xsd file.
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadXml(file_get_contents("../xml/testxml.xml"));
echo $doc->schemaValidateSource(file_get_contents("../xml/validation.xsd")) ? 'valid' : 'invalid';

Related

Using XSLT merge 2 XML, which come from PHP transformToXML

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);

DOMDocument load php file?

I have a php file, with xml headers, that i wish my script to load.
$doc = new DOMDocument();
$doc->load( 'xmlscript.php' );
Will not work, only files with *.xml. How can i make it load the file, like it is a xml file?
Since it's the data from db, that it shows in xml format, the file is in .php.
You'll have to get the output from the PHP file, and parse that.
ob_start();
include 'xmlscript.php';
$result = ob_get_clean();
$doc = new DOMDocument();
$doc -> loadXml($result);
Something like that may help.

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.

Parsing XML file through PHP while using XSLT as the main template file

I have a lots (500ish) xml files from An old ASP and VBscript that was running on an old windows server. The user could click a link to download the requested xml file, or click a link to view how the xml file will look, once its imported into their system...
If clicked to view the output, this opened a popup window were the xml filename is passed via URL & using the xslt template file this would display the output.
example url = /transform.php?action=transform&xmlProtocol=AC_Audiology.xml
Now were using PHP5 im trying to get something that resembles the same output.
we started looking into xslt_create(); but this is an old function from php4
I'm looking for the best method to deploy this.
The main php page should check & capture the $_GET['xmlProtocol'] value.
pass this to the xslt template page as data;
were it will be output in html.
a general point in the right direction would be great!
You can find the documentation (+examples) of the "new" XSL(T) extension at http://docs.php.net/xsl.
php
// Transform.php
if(isset($_GET['action']) && $_GET['action'] == 'transform') {
// obviously you would never trust the input and would validate first
$xml_file = AFunctionValidateAndGetPathToFile($_GET['xmlProtocol']);
// Load up the XML File
$xmlDoc = new DOMDocument;
$xmlDoc->load($xml_file);
// Load up the XSL file
$xslDoc = new DomDocument;
$xslDoc->load("xsl_template_file.xsl");
$xsl = new XSLTProcessor;
$xsl->importStyleSheet($xslDoc);
// apply the transformation
echo $xsl->transformToXml($xmlDoc);
}
I had a similar problem about two years ago. I was using PHP5 but needed to use xslt_create(); or an equivalent. Ultimately, I switched to PHP4.
You can probably set your server to use PHP5 everywhere except for files in a certain folder. I believe that's what I did so I could process XSL files using PHP4 but the majority of the site still used PHP5.
It's possible that things have changed in the last two years and PHP5 has better support for something like xslt_create(); ---- I haven't been following recent changes.
Hope this helps!

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