Sample code on using PHP and XSLT to transform XML

Lots of working codes using PHP are available in the internet for transforming the XML files using XSL.

$xsltrans = new DomDocument();
$xsltrans->load("XSL_file.xsl");
$xmlinput = new DomDocument();
$xmlinput->load("XML_file.xml");
$transobj = new XsltProcessor();
$transobj->registerPhpFunctions();
$xsltrans = $transobj->importStylesheet($xsltrans);
$result = $transobj->transformToDoc($xmlinput);
print $result->saveXML();

The above sample code will transform the "XML_file.xml" using the stylesheet "XSL_file.xsl". From the code you can see that two DOM objects are created. One is to load the XML file and the other is to load the XSL file.

The 'new DomDocument()' is used to create the DOM object. This object has the 'load' method, which is used to load the XML and the XSL file into them. Once this is done a XSLT processor instance is created with the command 'new XsltProcessor()'.

The importStylesheet method and the transformToDoc method of this processor instance are used to import the stylesheet and then do the transformation. Then the result is printed out.



.

SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.