Talk:XSLT

From BC$ MobileTV Wiki
Jump to: navigation, search

EXAMPLES

XSL

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"?>'''
        <class>
	  <student>Jack</student>
	  <student>Harry</student>
	  <student>Rebecca</student>
	  <teacher>Mr. Bean</teacher>
	</class>
Then, "transformClass.xsl" is:
	<?xml version="1.0" ?>
	<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

		<xsl:template match="teacher">
			<p><u><xsl:value-of select="."/></u></p>
		</xsl:template>
		
		<xsl:template match="student">
			<p><b><xsl:value-of select="."/></b></p>
		</xsl:template>
		<xsl:template match="/">
			<html>
			<body>
			<xsl:apply-templates/>
			</body>
			</html>
		</xsl:template>
	</xsl:stylesheet>
/pre>
* XSL Examples: http://www.w3schools.com/xsl/xsl_examples.asp


=== Java ===

<pre>
	import javax.xml.transform.*;
	import javax.xml.transform.stream.StreamResult;
	import javax.xml.transform.stream.StreamSource;
	import java.io.File;
	import java.io.IOException;
	import java.net.URISyntaxException;

	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")));
		}
	}

JavaScript

	/**
	 * 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();
		  xsltProcessor.importStylesheet(xsl);
		  var resultDocument = xsltProcessor.transformToFragment(xml,document);
		  document.getElementById('xslt').innerHTML = resultDocument;
		}
	}

PHP

  <?php
  // create a DOM document and load the XSL stylesheet
  $xsl = new DomDocument;
  $xsl->load('something.xsl');
  
  // import the XSL styelsheet into the XSLT process
  $xp->importStylesheet($xsl);

  // create a DOM document and load the XML datat
  $xml_doc = new DomDocument;
  $xml_doc->load('something.xml');

  //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);
  }
  ?>

Perl

Trillian Keyword Discovery API Example: http://keyworddiscovery.com/kd-api-examples.html

	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)
	  . '@api.keyworddiscovery.com/free.php';

	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

  • Python XSLT examples: