## XSL (eXtensible Stylesheet Language)
* XSL is a high-level functional language used to transform XML documents into various formats (XML, HTML etc.)
* XSL program consists of a set of TEMPLATE rules.
* Each rule consists of a pattern and a template.
- pattern (XPath expression) => where clause
- template => construct clause
* XSL processor starts from the root element and tries to apply a pattern to that node; If it succeeds, it executes the corresponding template.
* The template, when executed, usually instructs the processor to produce some XML result and to apply the templates
* Recursively on the node's children.
* An XSL style sheet is a valid XML document
### Sample XML Document
``catalog.xml``
```xml
Dark Side of the Moon
Pink Floyd
10.90
Space Oddity
David Bowie
9.90
Aretha: Lady Soul
Aretha Franklin
9.90
```
### Applying XSLT Stylesheets to XML Documents
There are three ways of applying an XSLT stylesheet to an XML
document:
1. Directly applying an XSLT processor to the XML document and the XSLT stylesheet; e.g. on command line (``libxml2`` tool shown here):
```bash
$ xsltproc page1.xsl bib.xml
```
2. Calling an XSLT processor from within a (Python or Java) program
```bash
macbook-pro:xsl raj$ more xslTransform.py
import sys
from lxml import etree
def xslTransform(xsl,xml):
xslt_root = etree.parse(xsl)
transform = etree.XSLT(xslt_root)
xml_root = etree.parse(xml)
result = transform(xml_root)
return result
def main():
print(xslTransform(sys.argv[1],sys.argv[2]))
main()
$ python3 xslTransform.py page1.xsl bib.xml
```
3. Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation
```xml
...
...
```
### The Root of the XSL Document (program)
The root element of the XSL document (program) should be one of the following:
```xml
...
...
```
or
```xml
...
...
```
The ``xsl`` namespace allows the XSL processor to distinguish
between XSL tags and tags of the result document
### How Does XSLT Work?
* An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML document)
* Each template has a ``match`` attribute that specifies to which source nodes the template can be applied
* The current source node is processed by applying a template that matches this node
* Processing always starts at the root (``/``)
### Templates
A template has the form
```xml
... Template content ...
```
The content of a template consists of
* XML elements and text (HTML etc) that are copied to the result
* XSL elements that are actually instructions
The pattern syntax is a subset of XPath
#### Simple Example
Hello World! (``p1.xsl``)
```xml
Hello World
```
Test this and subsequent examples on Python's http server:
```bash
$ python3 -m http.server
```
### XSL Processing
* Processing starts by applying a template that matches the root (/)
- If the given XSL stylesheet does not have a template that matches the root, then one is inserted by default (“Default Templates”)
* The XSL stylesheet must specify explicitly whether templates should be applied to descendants of the root/node
* It is done by putting inside a template the following instruction:
```xml
```
* Without the select attribute, this instruction processes all the children of the current node
#### Example with ``xsl:apply-templates`` (``p2.xsl``)
```xml
A CD!
```
### Default Templates
* XSL provides implicit built-in templates that match every element and text nodes
```xml
```
* Templates we write always override these built-in templates (when they match)
#### Interaction of Default and User Defined Templates (``p3.xsl``)
```xml
Hello World
```
* The default templates print the text in the leaves of the first and third CD
* The second template above prints the "Hello World" (this replaces the default template for this node!)
### The Most Frequently Used Elements of XSL
* The ``value-of`` element extracts the value of a node from the nodelist located by xpath-expression:
```xml
```
* The ``for-each`` element loops over all the nodes in the nodelist located by xpath-expression
```xml
```
* The ``if`` element is for conditional processing
```xml
```
### The ```` Element
```xml
```
* The XSL element ```` can be used to extract the value of an element that is selected from the source XML document
* The extracted value is added to the output stream
* The selected element is located by an XPath expression that appears as the value of the ``select`` attribute
#### Example for ``value-of`` (``p4.xsl``)
```xml
A CD Catalog
```
Note that only the first matched element is retrieved for each
````
### The ```` Element
```xml
```
* The element loops over all the nodes in the nodelist of the XPath expression that appears as the value of the ``select`` attribute
* The value of each node can be extracted by an ```` element
#### Example for ``for-each`` (``p5.xsl``)
```xml
A CD Catalog
```
Note that if we change
```xml
```
to
```xml
```
we will get only CDs which have a price less than 10.
#### Example for ``for-each`` (``p6.xsl``)
```xml
A CD Catalog
```
### The ```` Element
* The ```` element is used to sort the list of nodes that are looped over by the ```` element
* Thus, the ```` must appear inside the ```` element
* The looping is done in sorted order
#### Example for ``sort`` (``p7.xsl``)
```xml
A CD Catalog
```
### The ```` Element
* The ```` element is used for conditional processing
* The condition appears as the value of the test attribute, for example:
```xml
some output
```
* The elements inside the ```` element are processed if the condition is true. Processing the inside elements means
- Copying them into the output stream if they are not XSL elements, and
- Evaluating them if they are XSL elements
* If the value of the test attribute is just an XPath expression (i.e., without any comparison), then the test is satisfied if the nodelist of this XPath expression is not empty
#### Example for ``if`` (``p8.xsl``)
```xml
A CD Catalog
```
### Applying Templates Recursively
* The following example shows how to apply templates recursively
* Generally, it is possible (but not in this example) that more than one template matches the current source node
* The specification (www.w3.org/TR/xslt) describes (Section 5.5) which template should be chosen for application
```xml
A CD Catalog
Title:
Artist:
```
### Is Recursive Application of Templates Really Needed?
* The output of the previous example can also be generated by an XSL stylesheet that uses only one template that matches the root (and does not use the element ````)
* However, some tasks can only be done by applying templates recursively
- This typically happens when the structure of the source XML document is not known
### Recursive Template Application
* Suppose that we want to write an XSL stylesheet that generates an exact copy of the source XML document
* It is rather easy to do it when the structure of the source XML document is known
* Can we write an XSL stylesheet that does it for every possible XML document?
- Yes!
```xml
```
### Summary
* XSLT is a high-level transformation language
* Create core output once in XML format (using Servlets, JSP, etc.)
* Use XSLT to transform the core output as needed