From BC$ MobileTV Wiki
Jump to: navigation, search



Here is "school.xml", augmented with <?xml-stylesheet type="text/xsl" href="PATH_TO.xsl"?>:

	<?xml version="1.0" encoding="UTF-8"?>
	'''<?xml-stylesheet type="text/xsl" href="transformClass.xsl"?>'''
	  <teacher>Mr. Bean</teacher>
Then, "transformClass.xsl" is:
	<?xml version="1.0" ?>
	<xsl:stylesheet version="1.0" xmlns:xsl="">

		<xsl:template match="teacher">
			<p><u><xsl:value-of select="."/></u></p>
		<xsl:template match="student">
			<p><b><xsl:value-of select="."/></b></p>
		<xsl:template match="/">
* XSL Examples:

=== Java ===

	import javax.xml.transform.*;

	public class TestMain {
		public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
			TransformerFactory factory = TransformerFactory.newInstance();
			Source xslt = new StreamSource(new File("transform.xslt"));
			Transformer transformer = factory.newTransformer(xslt);

			Source text = new StreamSource(new File("input.xml"));
			transformer.transform(text, new StreamResult(new File("output.xml")));


	 * xslt
	 *   Process XML using specified XSL Transformations, then display the result in an HTML page
	 * NOTE: Relies on the browser's internal XSLT processor, 
	 *       which should be fairly standard, but you might 
	 *       want to expect bugs with old browsers
	function xslt(xmlFile, xslFile)
		var xml = loadXMLDoc(xmlFile);
		var xsl = loadXMLDoc(xslFile); //force it to be output as XML

	  // IE 6-8
		if (window.ActiveXObject)
		  var ex = xml.transformNode(xsl);
  		  document.getElementById('xslt').innerHTML = ex;
	  // FF, Chrome, Safari, Opera, IE9+
		else if (document.implementation && document.implementation.createDocument) 
		  var xsltProcessor = new XSLTProcessor();
		  var resultDocument = xsltProcessor.transformToFragment(xml,document);
		  document.getElementById('xslt').innerHTML = resultDocument;


  // create a DOM document and load the XSL stylesheet
  $xsl = new DomDocument;
  // import the XSL styelsheet into the XSLT process

  // create a DOM document and load the XML datat
  $xml_doc = new DomDocument;

  //Use this method to load in any optional parameters which you want to pass to the XSL transformation process. Note that with PHP version 5.0.0 you must load the parameters one at a time:
  $xp->setParameter($namespace, 'id1', 'value1');
  $xp->setParameter($namespace, 'id2', 'value2');

  //With PHP version 5.1.0 you can create an array of parameters and load them in one go:
  $params['id1'] = 'value1';
  $params['id2'] = 'value2';
  $xp->setParameter($namespace, $params);

  // transform the XML into HTML using the XSL file
  if ($html = $xp->transformToXML($xml_doc)) {
      echo $html;
  } else {
      trigger_error('XSL transformation failed.', E_USER_ERROR);


Trillian Keyword Discovery API Example:

	use CGI;
	use LWP;
	use XML::LibXSLT;
	use XML::LibXML;
	use URI::Escape;

	my $username = 'username';
	my $password = 'password';

	my $q = new CGI;

	my $ua = LWP::UserAgent->new;

	my $request_url = 'http://' . uri_escape($username) . ":" . uri_escape($password)
	  . '';

	my %params = $q->Vars;

	my $res = $ua->post($request_url,\%params);

	if ($res->is_success) {
	  print $q->header(-charset=>'utf8');

	  my $parser = XML::LibXML->new();
	  my $xslt = XML::LibXSLT->new();

	  my $xml_data = $parser->parse_string($res->content);
	  my $style_doc = $parser->parse_file('output_template.xsl');

	  my $stylesheet = $xslt->parse_stylesheet($style_doc);
	  my $results = $stylesheet->transform($xml_data);

	  print $stylesheet->output_string($results);
	} else {
	  print $q->header(-charset=>'utf8'),
		$q->start_html('Auth Error Error'),
		$q->h1('Auth Error'),
		'Server Setup Error: invalid authentication details';


  • Python XSLT examples: