1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in


From saxon

Jump to: navigation, search

Calling Saxon from PHP

There are three ways to call SAXON from PHP:

  • with the JavaBridge for PHP [reference needed]
  • from the command line.
  • as a (RESTful) web service

Each way has it's pros and cons.

The advantage of using the JavaBridge for PHP is that you get the best possible performance SAXON can deliver for PHP, as well as its full capabilities. The disadvantage - it could be arguable as to how easy installing the bridge and running SAXON is, especially if you don't know JAVA. Also, unlike other PHP extensions, stability is usually either in the form of "Runs perfectly, no bugs" or "crashes" i.e. if you can get it up and running, you'll be fine.

The advantage of using the command line is that you just "extract it and run it". It's as easy and stable as that. The disadvantage is that you lose very much on performance, it's hard to programatically act on errors and you can only use the options provided in the command line for SAXON.

Using a web service gives a clean separation of concerns: you run the XSLT transformations typically as Java servlets, and the PHP program invokes them by sending HTTP requests. They don't have to run on the same machine. Typically this will deliver a high throughput though there may be some penalty in terms of latency (user response time). And it may be difficult to achieve very close integration between the calling application and the XSLT code.

XML_XSLT2Processor is a command line wrapper for launching SAXON that uses the API of the PHP XSL extension to do so. It is aimed at providing the easiest possible way to launching SAXON, but it relies heavily on the command line for everything, including delivering error messages in an object oriented form, so its stability is questionable. It also doesn't (yet) deliver the full features of SAXON's command line. It's good if you just want to "play" with SAXON, but it's not really recommended for production use. If it doesn't prove good enough for you, you should really consider using the JavaBridge.

Following page was copied from http://uk2.php.net/xsl on 20 Oct 2007

pb at online-magazin dot at 11-Oct-2007 11:13
Calling the Saxon XSLT Processor is a very easy task to do!

You just need to do some simple tasks:

1. Install the JavaBridge for PHP

2. Download the freeware (B) Saxon distribution from http://saxon.sourceforge.net/

3. Put the jar files in a directory where you have access

4. Use this sample of code

Directory where the jar files are located
define("SAXON_DIR", $_SERVER['DOCUMENT_ROOT']."/saxonb8.9.0/");

include the jars

$sXslFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xsl"; The xsl file
$sXmlFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xml"; The xml file

$oXslSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXslFile);
$oXmlSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXmlFile);

$oFeatureKeys = new JavaClass("net.sf.saxon.FeatureKeys");

Create the Factory
$oTransformerFactory = new java("net.sf.saxon.TransformerFactoryImpl");

Disable source document validation
$oTransformerFactory->setAttribute($oFeatureKeys->SCHEMA_VALIDATION, 4);

Create a new Transformer
$oTransFormer = $oTransformerFactory->newTransformer($oXslSource);

Create a StreamResult to store the output
$oResultStringWriter = new java("java.io.StringWriter");
$oResultStream = new java("javax.xml.transform.stream.StreamResult", $oResultStringWriter);

$oTransFormer->transform($oXmlSource, $oResultStream);

Echo the output from the transformation
echo java_cast($oResultStringWriter->toString(), "string");

catch(JavaException $e){
echo java_cast($e->getCause()->toString(), "string");

5. Enjoy

This is working quite well.

Supplement by Michael Kay: I haven't tried this. As far as I can see, it shouldn't be necessary to include saxon8-dom.jar, and it shouldn't be necessary to disable schema validation.

Personal tools