XSLT
eXtensible Stylesheet Language Translation is act of applying a specially formatted file which contains an XSL translation specification for the content of a particular XML file, translating the context from raw XML format into a formatted HTML page (or other application/GUI element).
Contents
Structure
The structure of an XSLT requires three parts:
- A well-formed XML document we want to parse
- An XSL file for grabbing the desired data from the XML document and performing the transformation
- A target HTML file with a particular look & feel we want to generate and present to the end-us
These three components and the flow of a typical transformation is nicely summarized in the following diagram:
EXAMPLE
For example, with the following XML document for defining a shareable Contact list:
<AddressBook> <contact type="simple"> <name>John Doe</name> <title>CEO, Company Inc.</title> <email>john.doe@company.com</email> <phone>(123) 456-7890</phone> </contact> ... </AddressBook>
We can extract each contact's information via the XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template match="contact[@type='simple']"> <html xmlns="http://www.w3.org/1999/xhtml"> <title>business card</title> <body> <ul> <xsl:apply-templates select="name"/> <xsl:apply-templates select="title"/> <xsl:apply-templates select="email"/> <xsl:apply-templates select="phone"/> </ul> </body> </html> </xsl:template> <xsl:template match="contact/name"> <li> <h1><xsl:value-of select="text()"/></h1> </xsl:template> <xsl:template match="contact/title"> occupation: <h3><xsl:value-of select="text()"/></em></h3> </xsl:template> <xsl:template match="contact/email"> <p>email: <a href="mailto:{text()}"><tt><xsl:value-of select="text()"/></tt></a></p> </xsl:template> <xsl:template match="contact/phone"> <p>phone#: <a href="tel:{text()}"><xsl:value-of select="text()"/></a></p> <li> </xsl:template> </xsl:stylesheet>
This will output the following HTML to be displayed in the user's browser:
<html xmlns="http://www.w3.org/1999/xhtml"> <title>Address Book</title> <body> <ul> <li> <h1>John Doe</h1> <h3><em>CEO, Widget Inc.</em></h3> <p>email: <a href="mailto:john.doe@widget.com"><tt>john.doe@widget.com</tt></a></p> <p>phone: (202) 456-1414</p> </li> ... </ul> </body> </html>
Tools
XSLT Standard Library
- XSLT Standard Library: http://xsltsl.sourceforge.net/
XSLT Generator
- XSLTGen -- An Automatic XSLT Stylesheet Generator: http://ww2.cs.mu.oz.au/~jbailey/xsltgen/XSLTGen.htm
W3C XSLT Service
- W3C XSLT (auto-transformation) service: http://www.w3.org/2000/06/webdata/xslt
- W3schools -- Online XSLT Editor: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
http://www.w3.org/2000/06/webdata/xslt?xslfile=http://tomi.vanek.sk/xml/wsdl-viewer.xsl &xmlfile=http://198.164.40.210:8080/SAVOIR_MgmtServices/services/UserManagerWS?wsdl &transform=Submit
AJAXSLT
AJAXSLT is an implementation of XSL-T in JavaScript, intended for use in fat web pages, which are nowadays referred to as AJAX applications. Because XSL-T uses XPath, it is also an implementation of XPath that can be used independently of XSL-T.
- AJAXSLT (now deprecated): http://goog-ajaxslt.sourceforge.net/
- Google JS-Template (replacing AJAXSLT @ Google): http://code.google.com/p/google-jstemplate/
Resources
- XSLTV: http://ericandchar.com/xsltvgrid/
- A simple FOR loop in XSLT: http://snippets.dzone.com/posts/show/930
- XSLT - file for scraping RDF Semantics from xHTML files: http://lists.w3.org/Archives/Public/w3c-wai-er-ig/2001Jun/att-0017/rdfs2xhtml.xsl
- XSLT - file for converting from RDFS to xHTML: http://weborganics.co.uk/stylesheets/rdfs-xhtml.xsl
Java
- XSLT Processing with Java: http://onjava.com/pub/a/onjava/excerpt/java_xslt_ch5/index.html
- TransformUtil.java: http://java.codefetch.com/cache?url=http://www.apress.com/ApressCorporate/supplement/1/460/1590595203-2919.zip&path=jcb_package/src/jcb/util/TransformUtil.java
JavaScript
- Using the Mozilla JavaScript interface to XSL Transformations: https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations
PHP
- PHP Manual XSL: http://us2.php.net/xsl
- XML And XSL in PHP: http://www.phpro.org/tutorials/XML-And-XSL.html
- Using PHP 5's XSL extension to perform XSL Transformations: http://www.tonymarston.net/php-mysql/xsl.html
- PHP Server Side Scripting - Parsing/Accessing a page's HTML Anchors: http://www.webmasterworld.com/php/3620282.htm
- Using PHP and XSL to Transform XML into Web Content: http://devzone.zend.com/article/1302-Using-PHP-and-XSL-to-Transform-XML-into-Web-Content
- Code generation in XSLT 2.0, Part 2 -- Generate PHP with XSLT 2.0: http://www.ibm.com/developerworks/xml/library/x-xslphp2/index.html?ca=drs-x0905
- Transforming XML with XSLT and PHP: http://www.codewalkers.com/c/a/Miscellaneous/Transforming-XML-with-XSLT-and-PHP/
C
- The XSLT C library for GNOME: http://xmlsoft.org/XSLT/xsltproc2.html
- libxslt: http://xmlsoft.org/XSLT/
C#
- XML transformation using Xslt in C#: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
- Extending XSLT using C#: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=64
Tutorials
- Jeni's XSLT Pages: http://www.jenitennison.com/xslt/index.html
- W3 Schools XSL Tutorial: http://www.w3schools.com/xsl/xsl_w3celementref.asp
- Rescuing XSLT from Niche Status - A Gentle Introduction to XSLT through HTML Templates: http://www.xfront.com/rescuing-xslt.html
- IBM's "Generating internal HTML links with XSLT" tutorial: http://www.ibm.com/developerworks/library/x-tipxslt.html
- Convert Windows Media Player playlists to Winamp format using XSLT: http://blogs.techrepublic.com.com/programming-and-development/?p=3168
External Links
- wikipedia: XSL_Transformations
- A (very) simple XSLT test utility: http://www.codeproject.com/KB/XML/XSLT_Tester.aspx
- XML-to-string converter (An XML serializer implemented in XSLT): http://www.xmlportfolio.com/xml-to-string/
- XSL Formatter converts XML to PDF: http://www.antennahouse.com/product/axfo30/pdfoutput.htm
- Web Interface V3.0 for Formatter: http://www.antennahouse.com/product/webinterface/
- Five XSLT Basics: http://www.xml.com/pub/a/2003/11/26/learnXSLT.html
- 'Could not compile stylesheet' error - included xslt file path: http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14281584&tstart=0
- XSLT Questions and Answers: http://www.dpawson.co.uk/xsl/sect2/sect21.html
- Beginner XML / XSLT count() help needed: http://www.dreamincode.net/forums/topic/135747-beginner-xml-xslt-count-help-needed/
- XSLT Tutorial: http://www.learn-xslt-tutorial.com/
- xsltfunctions - fn:dateTime: http://www.xsltfunctions.com/xsl/fn_datetime.html
- EXSLT by example: http://www.ibm.com/developerworks/library/x-exslt.html
- Discover the Wonders of XSLT: http://www.java2s.com/Article/Java/XML/Discover_the_Wonders_of_XSLT.htm
- Tricky whitespace handling in XSLT: http://www.xmlplease.com/whitespace
- XSLT - Splitting and Manipulating Strings: http://www.xml.com/pub/a/2002/05/01/xslt-string.html
- XSLT String Manipulation - Remove spaces in Field Value for use in URL: http://blog-sharepoint.blogspot.com/2009/09/xslt-string-manipulation-replace-spaces.html
- Up-conversion using XSLT 2.0: http://www.saxonica.com/papers/ideadb-1.1/mhk-paper.xml
- XSLT - String and Number Manipulation: http://www.jenitennison.com/xslt/strings.xml
- XSLT and XPath Function Reference: http://docstore.mik.ua/orelly/xml/xslt/appc_01.htm
- Regular Expression Matching in XSLT 2: http://www.xml.com/pub/a/2003/06/04/tr.html
- Use XSLT to transform XML to Open XML: http://openxmldeveloper.org/articles/4283.aspx
- How does XSLT transform XML?: http://www.webreference.com/xml/resources/books/xsltreference/31290103.htm
- String replacement template: http://www.dpawson.co.uk/xsl/sect2/replace.html#d8766e43
- Transforming RDF/XML with XSLT: http://www.wasab.dk/morten/blog/archives/2004/05/30/transforming-rdfxml-with-xslt