Example two: Using XSLT to transform XML through Xalan 1.2
As a second example, we used the Xalan-java XSLT engine, which came from the Apache XML project, where we were able to transform XML source files using XSL. This will greatly facilitate our handling of documents and content management.
Before we start, we need to put the Xerces.jar and Xalan.jar files in the Java.class.path directory (these two files are contained in Xalan-java 1.2 and can be downloaded from the xml.apache.org).
The PHP program is as follows:
The function Xslt_transform () is an XML and xsl file parameter, in the form of a file name (such as: foo.xml) or a URL (such as: Http://localhost/foo.xml).
<?php
function Xslt_transform ($xml, $xsl) {
Create a Xsltprocessorfactory object. Xsltprocessorfactory is a Java
Class which manufactures the processor for performing transformations.
$XSLTProcessorFactory = new Java ("Org.apache.xalan.xslt.XSLTProcessorFactory");
Use the Xsltprocessorfactory method Getprocessor () to create a
New Xsltprocessor object.
$XSLTProcessor = $XSLTProcessorFactory->getprocessor ();
Use Xsltinputsource objects to provide input to the Xsltprocessor
Process () method for transformation. Create objects for both the
The XML source as the as is the XSL input source. Parameter of
Xsltinputsource is (in the case) a ' system identifier ' (URI) which
Can is an URL or filename. If The system identifier is a URL, it
Must be fully resolved.
$xmlID = new Java ("Org.apache.xalan.xslt.XSLTInputSource", $xml);
$stylesheetID = new Java ("Org.apache.xalan.xslt.XSLTInputSource", $xsl);
Create a StringWriter object for the output.
$stringWriter = new Java ("Java.io.StringWriter");
Create a Resulttarget object for the output with the Xsltresulttarget
Class. Parameter of Xsltresulttarget is (in the case) a ' character
Stream ', which is the StringWriter object.
$resultTarget = new Java ("Org.apache.xalan.xslt.XSLTResultTarget", $stringWriter);
Process input with the Xsltprocessors ' method process (). This
Method uses the XSL stylesheet to transform the XML input, placing
The result at the result target.
$XSLTProcessor->process ($xmlID, $stylesheetID, $resultTarget);
Use the Stringwriters ' method toString () to
Return the buffer ' s current value as a string to get the
Transformed result.
$result = $stringWriter->tostring ();
$stringWriter->close ();
return ($result);
}
?>
Once the function is defined, we can call it, and in the following routine, the variable $xml points to a URL string, $xsl as well. This example will display 5 of the latest phpbuilder.com article titles.
While this can be done in other ways, perhaps better, this example will give you a better idea of PHP invoking Java classes.
The tutorial is over, and hopefully you'll be able to learn something from this tutorial, and here are some links you can use:
http://www.php4win.de ~ A Great Win32 distribution of PHP
http://www.javasoft.com ~ Sun ' s Java release
http://www.jars.com ~ Start Searching for handy Java classes
http://www.gamelan.com ~ more Java Classes
http://www.technetcast.com/tnc_play_stream.html?stream_id=400 ~ Sam Ruby about PHP and Java integration at Open Source Con Vention (Audio)
http://xml.apache.org ~ Apache XML Project
HTTP://WWW.PHPBUILDER.COM/COLUMNS/JUSTIN20001025.PHP3 ~ Transforming XML with XSL using Sablotron
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.