XSL Languages

XSL Languages

It started with XSL and ended up with XSLT, XPath, and XSL-FO.


It Started with XSL

XSL stands for EXtensible Stylesheet Language.

The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.


CSS = HTML Style Sheets

HTML uses predefined tags and the meaning of the tags are well understood.

The <table> element in HTML defines a table - and a browser knows how to display it.

Adding styles to HTML elements is simple. Telling a browser to display an element in a special font or color, is easy with CSS.


XSL = XML Style Sheets

XML does not use predefined tags (we can use any tag-names we like), and the meaning of these tags are not well understood.

A

element could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it.

XSL describes how the XML document should be displayed!


XSL - More Than a Style Sheet Language

XSL consists of three parts:

  • XSLT - a language for transforming XML documents
  • XPath - a language for navigating in XML documents
  • XSL-FO - a language for formatting XML documents

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.

XPath is a language for navigating in XML documents.


What You Should Already Know

Before you continue you should have a basic understanding of the following:

  • HTML / XHTML
  • XML / XML Namespaces
  • XPath


What is XSLT?

  • XSLT stands for XSL Transformations
  • XSLT is the most important part of XSL
  • XSLT transforms an XML document into another XML document
  • XSLT uses XPath to navigate in XML documents
  • XSLT is a W3C Recommendation

XSLT = XSL Transformations

XSLT is the most important part of XSL.

XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.

With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.


XSLT Uses XPath

XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.


How Does it Work?

In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.

Nearly all major browsers have support for XML and XSLT.


Mozilla Firefox

As of version 1.0.2, Firefox has support for XML and XSLT (and CSS).


Mozilla

Mozilla includes Expat for XML parsing and has support to display XML + CSS. Mozilla also has some support for Namespaces.

Mozilla is available with an XSLT implementation.


Netscape

As of version 8, Netscape uses the Mozilla engine, and therefore it has the same XML / XSLT support as Mozilla.


Opera

As of version 9, Opera has support for XML and XSLT (and CSS). Version 8 supports only XML + CSS.


Internet Explorer

As of version 6, Internet Explorer supports XML, Namespaces, CSS, XSLT, and XPath.

Version 5 is NOT compatible with the official W3C XSL Recommendation.

Example study: How to transform XML into XHTML using XSLT.

The details of this example will be explained in the next chapter.


Correct Style Sheet Declaration

The root element that declares the document to be an XSL style sheet is or .

Note: and are completely synonymous and either can be used!

The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".


Start with a Raw XML Document

We want to transform the following XML document ("cdcatalog.xml") into XHTML:

  
    Empire Burlesque
    Bob Dylan
    USA
    Columbia
    10.90
    1985
  
.
.
.

Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.

Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.

Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text.


Create an XSL Style Sheet

Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  
    

My CD Collection

    
    
      
      
    
    
    
      
      
    
    
    
TitleArtist
  
  


Link the XSL Style Sheet to the XML Document

Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):

  
    Empire Burlesque
    Bob Dylan
    USA
    Columbia
    10.90
    1985
  
.
.
.

If you have an XSLT compliant browser it will nicely transform your XML into XHTML.

XSLT Element


An XSL style sheet consists of one or more set of rules that are called templates.

Each template contains rules to apply when a specified node is matched.


The Element

The element is used to build templates.

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

Ok, let's look at a simplified version of the XSL file from the previous chapter:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 
   

My CD Collection

   
     
       
       
     
     
       
       
     
   
TitleArtist
..
 
 

Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: .

The next element, , defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).

The element defines a template. The match="/" attribute associates the template with the root of the XML source document.

The content inside the element defines some HTML to write to the output.

The last two lines define the end of the template and the end of the style sheet.

The Element

The element can be used to extract the value of an XML element and add it to the output stream of the transformation:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 
   

My CD Collection

   
     
       
       
     
     
      
      
     
   
TitleArtist
 
 

Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

The Element

The XSL element can be used to select every XML element of a specified node-set:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  
    

My CD Collection

    
      
        
        
      
      
      
        
        
      
      
    
TitleArtist
  
  

Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories

Where to put the Sort Information

To sort the output, simply add an element inside the element in the XSL file:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  
    

My CD Collection

    
      
        
        
      
      
      
      
        
        
      
      
    
TitleArtist
  
  

Note: The select attribute indicates what XML element to sort on.

The Element

To put a conditional if test against the content of the XML file, add an element to the XSL document.

Syntax

expression">
  ...
  ...some output if the expression is true...
  ...


Where to Put the Element

To add a conditional test, add the element inside the element in the XSL file:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  
    

My CD Collection

    
      
        
        
      
      
      
        
          
          
        
      
      
    
TitleArtist
  
  

Note: The value of the required test attribute contains the expression to be evaluated.

The Element

Syntax

  expression">
    ... some output ...
  
  
    ... some output ....
  


Where to put the Choose Condition

To insert a multiple conditional test against the XML file, add the , , and elements to the XSL file:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  
    

My CD Collection

    
      
        
        
      
      
      
        
        
          
            
          
          
            
          
        
      
      
    
TitleArtist
            
  
  

The Element

The element applies a template to the current element or to the current element's child nodes.

If we add a select attribute to the element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

Look at the following XSL style sheet:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My CD Collection

 

 

Title: 

Artist: 

No comments:

Post a Comment