How to transform an XML Document with XSLT into a new File in PHP

Cloudmersive
2 min readApr 15, 2020

--

Using XSLT to transform an XML document can open up quite a range of useful options, but to do this from within PHP can be a bit of a pain. We’ve got you covered on that, however. Through the simple use of one of our APIs, you’ll be up and running in no time.

The first phase of our process is simply to install our PHP client with this command here:

composer require cloudmersive/cloudmersive_document_convert_api_client

Proceed with calling the convertDataXmlTransformWithXsltToXml function, using this set of code here:

<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ConvertDataApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input XML file to perform the operation on.$transform_file = "/path/to/file"; // \SplFileObject | Input XSLT file to use to transform the input XML file.try {$result = $apiInstance->convertDataXmlTransformWithXsltToXml($input_file, $transform_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ConvertDataApi->convertDataXmlTransformWithXsltToXml: ', $e->getMessage(), PHP_EOL;}?>

With that done, we just need to provide our XML and XSLT files, then done faster than you can say Bob’s your uncle.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet