Php&java (iii)

Source: Internet
Author: User
Tags object return string tostring xsl xsl file xsl stylesheet xslt
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.

<?php

$xml = "Http://www.phpbuilder.com/rss_feed.php?type=articles&limit=5";
$xsl = "http://www.soeterbroek.com/code/xml/rss_html.xsl";
$out = Xslt_transform ($xml, $xsl);
Echo $out;

?>

If you run the program on the local machine, you must make sure that your function argument points to the correct filename.

<?php

$xml = "/web/htdocs/xml_java/rss_feed.xml";
$xsl = "/web/htdocs/xml_java/rss_html.xsl";
$out = Xslt_transform ($xml, $xsl);
Echo $out;

?>

While this can be done in other ways, perhaps better, this example will give you a better idea of PHP invoking Java classes.


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.