Difference between revisions of "XSLT"

From BC$ MobileTV Wiki
Jump to: navigation, search
 
(82 intermediate revisions by 8 users not shown)
Line 1: Line 1:
''e'''X'''tensible '''S'''tylesheet '''L'''anguage '''T'''ranslation'' 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).
+
[[Image:Xslt.png|right|105px]]
 +
''e'''X'''tensible '''S'''tylesheet '''L'''anguage '''T'''ranslation'' 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 or format, such as a different [[XML]], extending basic [[XML]] up to [[RDF]] or simplifying [[XML]] down to [[CSV]] or plaintext values).
  
  
 +
== Specifications ==
  
 +
* XSLT: http://www.w3.org/TR/xslt | [http://www.w3.org/TR/xslt11/ v1.1] | [http://www.w3.org/TR/xslt20/ v2.0] | [http://www.w3.org/TR/xslt-30/ v3.0]
 +
* XQuery 1.0 and XPath 2.0 Functions and Operators: http://www.w3.org/TR/xpath-functions/
 +
* XSLT Security: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms763800(v=vs.85)
  
== Resources ==
 
  
* W3 Schools XSL Tutorial: http://www.w3schools.com/xsl/xsl_w3celementref.asp
 
* PHP Manual XSL: http://us2.php.net/xsl
 
* IBM's "Generating internal HTML links with XSLT" tutorial: http://www.ibm.com/developerworks/library/x-tipxslt.html
 
* [[wikipedia: XSL_Transformations]]
 
  
 +
== Structure ==
  
=== AJAXSLT ===
+
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
  
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/
+
These three components and the flow of a typical transformation is nicely summarized in the following diagram:
* Google JS-Template (replacing AJAXSLT @ Google): http://code.google.com/p/google-jstemplate/
+
  
 +
[[Image:XSLT.gif|center|thumb|315px|Transforming an XML document to HTML]]
  
  
=== XSLT Standard Library ===
 
  
* XSLT Standard Library: http://xsltsl.sourceforge.net/
+
== EXAMPLES ==
  
  
 +
=== Match ===
  
 +
For example, with the following [[XML]] document for defining a shareable Contact list:
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8" ?>
 +
<AddressBook>
 +
  <contact type="friend">
 +
    <name>Phil Hart</name>
 +
    <title>Social Worker, Federal Government of Countria</title>
 +
    <email>phil.hart@countria.gov</email>
 +
    <phone extension="3490">+23 (81) 892-2387</phone>
 +
  </contact>
 +
  <contact type="boss">
 +
    <name>John Doe</name>
 +
    <title>CEO, Company Inc.</title>
 +
    <email>john.doe@company.com</email>
 +
    <phone>+1 (123) 456-7890</phone>
 +
  </contact> 
 +
  <contact type="friend">
 +
    <name>Barb Boe</name>
 +
    <title>Secretary, Company Z Inc.</title>
 +
    <email>barb.boe@companyz.com</email>
 +
    <phone>+1 (98) 765-4321</phone>
 +
  </contact> 
 +
  ...
 +
</AddressBook>
 +
</pre>
 +
 +
 +
We can extract the information of each ''friend'' only, leaving out ''co-workers'', ''colleagues'', ''etc'' via the following [[XSL]]:
 +
<pre>
 +
<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='friend']">
 +
    <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="title">
 +
    occupation: <h3><xsl:value-of select="text()"/></em></h3>
 +
  </xsl:template>
 +
  <xsl:template match="email">
 +
    <p>email: <a href="mailto:{text()}"><tt><xsl:value-of select="text()"/></tt></a></p>
 +
  </xsl:template>
 +
  <xsl:template match="phone">
 +
    <xsl:variable name="contactphone" select='text()' />
 +
    <xsl:variable name="extension" select='@extension' />
 +
      <xsl:variable name="thephone">
 +
        <xsl:call-template name="replaceCharsInString">
 +
          <xsl:with-param name="stringIn" select="string($thephone)"/>
 +
          <xsl:with-param name="charsIn" select=" "/>
 +
          <xsl:with-param name="charsOut" select="-"/>
 +
        </xsl:call-template>
 +
      </xsl:variable>
 +
      <xsl:variable name="formatphonenum">
 +
        <xsl:call-template name="replaceCharsInString">
 +
          <xsl:with-param name="stringIn" select="string($thephone)"/>
 +
          <xsl:with-param name="charsIn" select="("/>
 +
          <xsl:with-param name="charsOut" select=""/>
 +
        </xsl:call-template>
 +
      </xsl:variable>
 +
      <xsl:variable name="phonenum">
 +
        <xsl:call-template name="replaceCharsInString">
 +
          <xsl:with-param name="stringIn" select="string($formatphonenum)"/>
 +
          <xsl:with-param name="charsIn" select=")"/>
 +
          <xsl:with-param name="charsOut" select=""/>
 +
        </xsl:call-template>
 +
      </xsl:variable>
 +
    <xsl:choose>
 +
      <xsl:when test="extension !=null">       
 +
        <p>phone#: <a href="tel:{phonenum}"><xsl:value-of select="phonenum"/>ext. <xsl:value-of select="extension" /></a></p>
 +
      </xsl:when>
 +
      <xsl:otherwise>
 +
        <p>phone#: <a href="tel:{phonenum}"><xsl:value-of select="phonenum"/></a></p>
 +
      </xsl:otherwise>
 +
    </xsl:choose>
 +
    <li>
 +
  </xsl:template>
 +
 +
  <!-- a useful template for string replacement -->
 +
      <xsl:template name="replaceCharsInString">
 +
        <xsl:param name="stringIn"/>
 +
        <xsl:param name="charsIn"/>
 +
        <xsl:param name="charsOut"/>
 +
        <xsl:choose>
 +
          <xsl:when test="contains($stringIn,$charsIn)">
 +
            <xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
 +
            <xsl:call-template name="replaceCharsInString">
 +
              <xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
 +
              <xsl:with-param name="charsIn" select="$charsIn"/>
 +
              <xsl:with-param name="charsOut" select="$charsOut"/>
 +
            </xsl:call-template>
 +
          </xsl:when>
 +
          <xsl:otherwise>
 +
            <xsl:value-of select="$stringIn"/>
 +
          </xsl:otherwise>
 +
        </xsl:choose>
 +
      </xsl:template>
 +
</xsl:stylesheet>
 +
</pre>
 +
(NOTE: this is a little more complicated because it also reformats some of the data to make a telephone number follow the [http://tools.ietf.org/html/rfc3966#section-5.3 tel protocol])
 +
 +
In order to call a stylesheet for rendering an XML file's data, simply add the following directive to the XML file directly below the header:
 +
<?xml-stylesheet type="text/xsl" href="contacts.xsl"?>
 +
 +
 +
This will output the following HTML to be displayed in the user's browser:
 +
<pre>
 +
<html xmlns="http://www.w3.org/1999/xhtml">
 +
<title>Address Book</title>
 +
<body>
 +
  <ul>
 +
    <li>
 +
      <h1>Phil Hrat</h1>
 +
      <h3><em>Social Worker, Federal Government of Countria</em></h3>
 +
      <p>email: <a href="mailto:phil.hart@countria.gov"><tt>phil.hart@countria.gov</tt></a></p>
 +
      <p>phone: <a href="tel:+23-1-81-892-2387,3490">+23 (81) 892-2387 ext.3490</a></p>
 +
    </li>
 +
    <li>
 +
      <h1>Barb Boe</h1>
 +
      <h3><em>Secretary, Company Z Inc.</em></h3>
 +
      <p>email: <a href="mailto:barb.boe@companyz.com"><tt>barb.boe@companyz.com</tt></a></p>
 +
      <p>phone: <a href="tel:+1-98-765-4321">+1 (81) 892-2387</a></p>
 +
    </li>
 +
    ...
 +
  </ul>
 +
</body>
 +
</html>
 +
</pre>
 +
<ref>A quick XSLT tutorial - Building an XSL Address Book: http://bcmoney-mobiletv.com/blog/2009/08/21/a-quick-xslt-tutorial/</ref>
 +
 +
 +
 +
=== Functions ===
 +
 +
There are a number of additional functions available in all XSLT 2.0-compatible processing/transformation engines.
 +
 +
* '''XSLT 2.0 Functions: http://www.xsltfunctions.com/''' (a summary of functions with examples)<ref>XPath, XQuery, and XSLT Functions: http://www.w3schools.com/xpath/xpath_functions.asp</ref><ref>XSLT 2.0 and XPath 2.0 Functions: http://saxon.sourceforge.net/saxon7.8/functions.html</ref>
 +
 +
In the following example, we extract the characters which occur before a space (i.e. first word in the matched data) for TITLE and the six characters which occur after the fourth character (i.e. last name of the artis):
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 +
<xsl:template match="/">
 +
  <html>
 +
  <body>
 +
    <h2>My CD Collection</h2>
 +
    <table border="1">
 +
      <tr bgcolor="#9acd32">
 +
        <th>Title</th>
 +
        <th>Artist</th>
 +
      </tr>
 +
      <tr>
 +
        <td><xsl:value-of select="substring-before(/catalog/cd/title,' ')"/></td>
 +
        <td><xsl:value-of select="substring(/catalog/cd/artist, 4, 6)"/></td>
 +
      </tr>
 +
    </table>
 +
  </body>
 +
  </html>
 +
</xsl:template>
 +
</xsl:stylesheet>
 +
</pre>
 +
 +
To apply the same (admittedly odd in this case) string operations to all items in the XML, it would become:
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 +
<xsl:template match="/">
 +
  <html>
 +
  <body>
 +
    <h2>My CD Collection</h2>
 +
    <table border="1">
 +
      <tr bgcolor="#9acd32">
 +
        <th>Title</th>
 +
        <th>Artist</th>
 +
      </tr>
 +
      <xsl:for-each select="catalog/cd">
 +
      <tr>
 +
        <td><xsl:value-of select="substring-before(title,' ')"/></td>
 +
        <td><xsl:value-of select="substring(artist, 4, 6)"/></td>
 +
      </tr>
 +
      </xsl:for-each>
 +
    </table>
 +
  </body>
 +
  </html>
 +
</xsl:template>
 +
</xsl:stylesheet>
 +
</pre>
 +
<ref>W3schools Try-It-Here -- using CD catalog XML: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2 (paste the above on the right)</ref>
 +
(NOTE: to perform the same string operations on ALL
 +
 +
 +
=== xsltproc ===
 +
 +
The XSLT processing tool (''xsltproc'') can be used to convert/transform any XML file, whether you have a specific XSLT or not.
 +
 +
For instance, to convert an XML file when you don't have a specific XSLT you can get the :
 +
xsltproc input.xml -o output.html
 +
 +
Whereas if you have a specific XSLT to process the source XML data with, use:
 +
xsltproc transformer.xsl input.xml > output.xml
 +
 +
<ref>XSLT processing with ''xsltproc'': http://fhoerni.free.fr/comp/xslt.html</ref>
 +
<ref>Tricks from the Command Line: xsltproc and xmllint: https://www.soliantconsulting.com/blog/filemaker-xsltproc-xmllint/</ref>
  
  
Line 33: Line 249:
 
== Tools ==
 
== Tools ==
  
 +
* XSLT Fiddle: https://xsltfiddle.liberty-development.net/3Nqn5Yp
 +
* '''JS Transformer: http://bcmoney-mobiletv.com/blog/2010/12/08/js-transformer/'''
 +
*
 +
 +
=== XSLT Generator ===
  
 +
* XSLTGen -- An Automatic XSLT Stylesheet Generator: http://ww2.cs.mu.oz.au/~jbailey/xsltgen/XSLTGen.htm
 +
* XSLT Tester: http://www.perfectxml.com/xsltester.asp (IE-only for client-side)
  
=== W3C XSLT Service ===
+
=== XSL Transformation Service ===
  
* '''W3C XSLT (auto-transformation) service: http://www.w3.org/2000/06/webdata/xslt'''
+
* Online XSLT: http://www.w3.org/2005/08/online_xslt/
 +
* '''W3C XSLT (auto-transformation) service: http://www.w3.org/2000/06/webdata/xslt''' (same as above, only can be used directly as a Transformation Web Service)
 +
* In-line XSLT processor: http://chris.photobooks.com/xml/default.htm
 
* W3schools -- Online XSLT Editor: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
 
* W3schools -- Online XSLT Editor: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
 +
[http://services.w3.org/xslt?xslfile=http://tomi.vanek.sk/xml/wsdl-viewer.xsl&xmlfile=http://soap.amazon.com/schemas2/AmazonWebServices.wsdl&transform=Submit EXAMPLE #1 - Amazon Web Services SOAP v1]
  
[http://www.w3.org/2000/06/webdata/xslt?xslfile=http://tomi.vanek.sk/xml/wsdl-viewer.xsl&amp;xmlfile=http://198.164.40.210:8080/SAVOIR_MgmtServices/services/UserManagerWS?wsdl&amp;transform=Submit Sample call]:
+
[http://services.w3.org/xslt?xslfile=http://tomi.vanek.sk/xml/wsdl-viewer.xsl&xmlfile=http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl&transform=Submit EXAMPLE #2 - Amazon Web Services SOAP v2]
http://www.w3.org/2000/06/webdata/xslt?xslfile=http://tomi.vanek.sk/xml/wsdl-viewer.xsl &amp;xmlfile=http://198.164.40.210:8080/SAVOIR_MgmtServices/services/UserManagerWS?wsdl &amp;transform=Submit
+
  
  
 +
== Resources ==
  
 +
* XSLT Standard Library: http://xsltsl.sourceforge.net/
 +
* '''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
 +
* XML-to-string converter: http://lenzconsulting.com/xml-to-string/
  
  
 +
=== 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
  
== Tutorials ==
+
=== JavaScript ===
  
* '''Jeni's XSLT Pages: http://www.jenitennison.com/xslt/index.html'''
+
* Using the Mozilla JavaScript interface to XSL Transformations: https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations
 +
 
 +
==== 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/
 +
 
 +
=== 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
 
* 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
 
* 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
* Using PHP 5's XSL extension to perform XSL Transformations: http://www.tonymarston.co.uk/php-mysql/xsl.html
 
 
* 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
 
* 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
 +
* '''A quick XSLT tutorial''': http://bcmoney-mobiletv.com/blog/2009/08/21/a-quick-xslt-tutorial/
 +
* 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
 +
* Introduction to XSLT 3.0: http://www.stylusstudio.com/tutorials/intro-xslt-3.html
 +
* Transforming XML Data with XSLT (applying a transform in Java): https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
 +
* Java XML Tutorial - Java XSLT: http://www.java2s.com/Tutorials/Java/Java_XML/0200__Java_XSLT_Intro.htm
 +
* Real-world XSLT example for converting Excel spreadsheet to CRA Tax XML format: https://www.tek-tips.com/viewthread.cfm?qid=1445878
 +
* XSLT Processing with Java - System Identifiers, Files, and URLs: http://www.onlamp.com/pub/a/onjava/excerpt/java_xslt_ch5/index.html?page=5
 +
* Link the XSL Style Sheet to the XML Document: https://www.w3schools.com/xml/xsl_transformation.asp#midcontentadcontainer
 +
<ref>'''How to link up XML file with XSLT file?: https://stackoverflow.com/questions/3456697/how-to-link-up-xml-file-with-xslt-file'''</ref><ref>Using inline XSLT for an XML file: https://stackoverflow.com/questions/7132106/using-inline-xslt-for-an-xml-file</ref><ref>How to embed XSL into XML file: https://stackoverflow.com/questions/32533660/how-to-embed-xsl-into-xml-file</ref>
 +
* Producing a new line in XSLT: https://stackoverflow.com/questions/723226/producing-a-new-line-in-xslt
 +
* XSLT &lt;xsl:apply-templates> Element: https://www.w3schools.com/xml/xsl_apply_templates.asp
 +
* XSLT &lt;xsl:call-template>https://www.w3schools.com/xml/ref_xsl_el_call-template.asp
 +
* XSL changing spaces in a link to %20: https://stackoverflow.com/questions/4491216/xsl-changing-spaces-in-a-link-to-20
 +
<ref>How to replace the space with some string from all the text values in XML?: https://stackoverflow.com/questions/2318118/xslt-1-0-how-to-replace-the-space-with-some-string-from-all-the-text-values-in</ref>
 +
<ref>XSLT version 1 URL encoding: https://stackoverflow.com/questions/2425516/xslt-version-1-url-encoding</ref>
 +
* Output result of xsl:value-of in an xml attribute: https://stackoverflow.com/questions/283858/output-result-of-xslvalue-of-in-an-xml-attribute
 +
* XML to XML transformation with XSLT in Firefox & IE: https://stackoverflow.com/questions/13659802/xml-to-xml-transformation-with-xslt-in-firefox-and-ie
 +
* XSLT ''&lt;xsl:output>'' support in main browsers: https://stackoverflow.com/questions/15851114/xslt-xsloutput-support-in-main-browsers
 +
* XML transform XSLT with namespaces: https://stackoverflow.com/questions/48320558/xml-transform-xslt-with-namespaces?noredirect=1&lq=1
 +
* XSLT Transform XML with Namespaces: https://stackoverflow.com/questions/1730875/xslt-transform-xml-with-namespaces
 +
* Convert XML file to RDF/XML using XSLT: https://stackoverflow.com/questions/25959107/convert-xml-file-to-rdf-xml-using-xslt
  
 
== External Links ==
 
== External Links ==
  
 +
* [[wikipedia: XSL_Transformations]]
 +
* [[wikipedia: Saxon_XSLT#Features]]
 +
* What kind of language is XSLT?: http://www.ibm.com/developerworks/library/x-xslt/
 +
* Improve your XSLT coding five ways: http://www.ibm.com/developerworks/library/x-xslt5/
 +
* XSLT processor command line: http://linux.byexamples.com/archives/463/xslt-processor-command-line/
 +
* Java Command Line XSLT Transformation: http://numberformat.wordpress.com/2010/03/20/java-command-line-xslt-transformation/
 +
* Introducing XSL, XSLT, and XPath in JAVA: http://docs.oracle.com/javase/tutorial/jaxp/xslt/intro.html
 +
* 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/
 
* 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
 
* 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/
 
* 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
 
* 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
 +
* How to view XML files in a web browser -- Turn XML files into something more useful with XSLT: https://opensource.com/article/18/12/xml-browser (transforming [https://www.scribus.net/ Scribus] & US [https://data.cms.gov/ Medicare/Medicaide dataset] examples)<ref>Medicare/Medicaide - Provider Datasets: https://data.cms.gov/provider-data/search | [https://data.cms.gov/api/views/s2uc-8wxp/rows.xml?accessType=DOWNLOAD DEMO] | [https://dev.socrata.com/foundry/data.cms.gov/s2uc-8wxp DOCS]</ref>
  
  
 +
== References ==
  
 
+
<references />
  
 
== See Also ==
 
== See Also ==
  
 
[[XSL]] | [[XML]]
 
[[XSL]] | [[XML]]
 +
 +
[[Category:Query Language]]

Latest revision as of 07:02, 15 March 2021

Xslt.png

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 or format, such as a different XML, extending basic XML up to RDF or simplifying XML down to CSV or plaintext values).


Specifications


Structure

The structure of an XSLT requires three parts:

  1. A well-formed XML document we want to parse
  2. An XSL file for grabbing the desired data from the XML document and performing the transformation
  3. 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:

Transforming an XML document to HTML


EXAMPLES

Match

For example, with the following XML document for defining a shareable Contact list:

 <?xml version="1.0" encoding="UTF-8" ?>
 <AddressBook>
   <contact type="friend">
     <name>Phil Hart</name>
     <title>Social Worker, Federal Government of Countria</title>
     <email>phil.hart@countria.gov</email>
     <phone extension="3490">+23 (81) 892-2387</phone>
   </contact>
   <contact type="boss">
     <name>John Doe</name>
     <title>CEO, Company Inc.</title>
     <email>john.doe@company.com</email>
     <phone>+1 (123) 456-7890</phone>
   </contact>   
   <contact type="friend">
     <name>Barb Boe</name>
     <title>Secretary, Company Z Inc.</title>
     <email>barb.boe@companyz.com</email>
     <phone>+1 (98) 765-4321</phone>
   </contact>  
   ... 
 </AddressBook>


We can extract the information of each friend only, leaving out co-workers, colleagues, etc via the following 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='friend']">
     <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="title">
     occupation: <h3><xsl:value-of select="text()"/></em></h3>
   </xsl:template>
   <xsl:template match="email">
     <p>email: <a href="mailto:{text()}"><tt><xsl:value-of select="text()"/></tt></a></p>
   </xsl:template>
   <xsl:template match="phone">
     <xsl:variable name="contactphone" select='text()' />
     <xsl:variable name="extension" select='@extension' />
      <xsl:variable name="thephone">
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" select="string($thephone)"/>
          <xsl:with-param name="charsIn" select=" "/>
          <xsl:with-param name="charsOut" select="-"/>
        </xsl:call-template>
      </xsl:variable> 
      <xsl:variable name="formatphonenum">
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" select="string($thephone)"/>
          <xsl:with-param name="charsIn" select="("/>
          <xsl:with-param name="charsOut" select=""/>
        </xsl:call-template>
      </xsl:variable> 
      <xsl:variable name="phonenum">
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" select="string($formatphonenum)"/>
          <xsl:with-param name="charsIn" select=")"/>
          <xsl:with-param name="charsOut" select=""/>
        </xsl:call-template>
      </xsl:variable>
     <xsl:choose>
      <xsl:when test="extension !=null">        
        <p>phone#: <a href="tel:{phonenum}"><xsl:value-of select="phonenum"/>ext. <xsl:value-of select="extension" /></a></p>
      </xsl:when>
      <xsl:otherwise>
        <p>phone#: <a href="tel:{phonenum}"><xsl:value-of select="phonenum"/></a></p>
      </xsl:otherwise>
     </xsl:choose>
     <li>
   </xsl:template>

   <!-- a useful template for string replacement -->
      <xsl:template name="replaceCharsInString">
        <xsl:param name="stringIn"/>
        <xsl:param name="charsIn"/>
        <xsl:param name="charsOut"/>
        <xsl:choose>
          <xsl:when test="contains($stringIn,$charsIn)">
            <xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
            <xsl:call-template name="replaceCharsInString">
              <xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
              <xsl:with-param name="charsIn" select="$charsIn"/>
              <xsl:with-param name="charsOut" select="$charsOut"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$stringIn"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
 </xsl:stylesheet>

(NOTE: this is a little more complicated because it also reformats some of the data to make a telephone number follow the tel protocol)

In order to call a stylesheet for rendering an XML file's data, simply add the following directive to the XML file directly below the header:

<?xml-stylesheet type="text/xsl" href="contacts.xsl"?>


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>Phil Hrat</h1>
       <h3><em>Social Worker, Federal Government of Countria</em></h3>
       <p>email: <a href="mailto:phil.hart@countria.gov"><tt>phil.hart@countria.gov</tt></a></p>
       <p>phone: <a href="tel:+23-1-81-892-2387,3490">+23 (81) 892-2387 ext.3490</a></p>
     </li>
     <li>
       <h1>Barb Boe</h1>
       <h3><em>Secretary, Company Z Inc.</em></h3>
       <p>email: <a href="mailto:barb.boe@companyz.com"><tt>barb.boe@companyz.com</tt></a></p>
       <p>phone: <a href="tel:+1-98-765-4321">+1 (81) 892-2387</a></p>
     </li>
     ...
   </ul>
 </body>
 </html>

[1]


Functions

There are a number of additional functions available in all XSLT 2.0-compatible processing/transformation engines.

In the following example, we extract the characters which occur before a space (i.e. first word in the matched data) for TITLE and the six characters which occur after the fourth character (i.e. last name of the artis):

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td><xsl:value-of select="substring-before(/catalog/cd/title,' ')"/></td>
        <td><xsl:value-of select="substring(/catalog/cd/artist, 4, 6)"/></td>
      </tr>
    </table>
  </body>
  </html>
 </xsl:template>
 </xsl:stylesheet>

To apply the same (admittedly odd in this case) string operations to all items in the XML, it would become:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="substring-before(title,' ')"/></td>
        <td><xsl:value-of select="substring(artist, 4, 6)"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
 </xsl:template>
 </xsl:stylesheet>

[4] (NOTE: to perform the same string operations on ALL


xsltproc

The XSLT processing tool (xsltproc) can be used to convert/transform any XML file, whether you have a specific XSLT or not.

For instance, to convert an XML file when you don't have a specific XSLT you can get the :

xsltproc input.xml -o output.html

Whereas if you have a specific XSLT to process the source XML data with, use:

xsltproc transformer.xsl input.xml > output.xml

[5] [6]



Tools

XSLT Generator

XSL Transformation Service

EXAMPLE #1 - Amazon Web Services SOAP v1

EXAMPLE #2 - Amazon Web Services SOAP v2


Resources


Java

JavaScript

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.

PHP

C

C#


Tutorials

[7][8][9]

[10] [11]

External Links


References

  1. A quick XSLT tutorial - Building an XSL Address Book: http://bcmoney-mobiletv.com/blog/2009/08/21/a-quick-xslt-tutorial/
  2. XPath, XQuery, and XSLT Functions: http://www.w3schools.com/xpath/xpath_functions.asp
  3. XSLT 2.0 and XPath 2.0 Functions: http://saxon.sourceforge.net/saxon7.8/functions.html
  4. W3schools Try-It-Here -- using CD catalog XML: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2 (paste the above on the right)
  5. XSLT processing with xsltproc: http://fhoerni.free.fr/comp/xslt.html
  6. Tricks from the Command Line: xsltproc and xmllint: https://www.soliantconsulting.com/blog/filemaker-xsltproc-xmllint/
  7. How to link up XML file with XSLT file?: https://stackoverflow.com/questions/3456697/how-to-link-up-xml-file-with-xslt-file
  8. Using inline XSLT for an XML file: https://stackoverflow.com/questions/7132106/using-inline-xslt-for-an-xml-file
  9. How to embed XSL into XML file: https://stackoverflow.com/questions/32533660/how-to-embed-xsl-into-xml-file
  10. How to replace the space with some string from all the text values in XML?: https://stackoverflow.com/questions/2318118/xslt-1-0-how-to-replace-the-space-with-some-string-from-all-the-text-values-in
  11. XSLT version 1 URL encoding: https://stackoverflow.com/questions/2425516/xslt-version-1-url-encoding
  12. Medicare/Medicaide - Provider Datasets: https://data.cms.gov/provider-data/search | DEMO | DOCS

See Also

XSL | XML