Showing posts with label XSL. Show all posts
Showing posts with label XSL. Show all posts

Thursday, June 13, 2013

XSL and XSLT

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 = Style Sheets for HTML

HTML uses predefined tags, and the meaning of each tag is well understood.
The <table> tag in HTML defines a table - and a browser knows how to display it.
Adding styles to HTML elements are simple. Telling a browser to display an element in a special font or color, is easy with CSS. 

XSL = Style Sheets for XML

XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag isnot well understood.
A <table> tag 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

This Tutorial is About XSLT

The rest of this tutorial is about XSLT - the language for transforming XML documents.
To learn more about XPath and XSL-FO, visit our XPath Tutorial and our XSL-FO Tutorial.
XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.


          XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.

                     XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other objects such as HTML for web pages, plain text or into XSL Formatting Objects which can then be converted to PDF, PostScript and PNG.

                   The original document is not changed; rather, a new document is created based on the content of an existing one.Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used, for example relational database tables, or geographical information systems.

                   XSLT is a Turing-complete language, meaning it can specify any computation that can be performed by a computer.

XSL

What is XSL?

XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type. XSL shares the functionality and is compatible with CSS2 (although it uses a different syntax). It also adds:
  • A transformation language for XML documents: XSLT. Originally intended to perform complex styling operations, like the generation of tables of contents and indexes, it is now used as a general purpose XML processing language. XSLT is thus widely used for purposes other than XSL, like generating HTML web pages from XML data.
  • Advanced styling features, expressed by an XML document type which defines a set of elements called Formatting Objects, and attributes (in part borrowed from CSS2 properties and adding more complex ones.

How Does It Work?

Styling requires a source XML documents, containing the information that the style sheet will display and the style sheet itself which describes how to display a document of a given type.
The following shows a sample XML file and how it can be transformed and rendered.

The XML file

<scene>
  <FX>General Road Building noises.</FX>
  <speech speaker="Prosser">
    Come off it Mr Dent, you can't win
    you know. There's no point in lying
    down in the path of progress.
  </speech>
  <speech speaker="Arthur">
    I've gone off the idea of progress.
    It's overrated
  </speech>
</scene>
This XML file doesn't contain any presentation information, which is contained in the stylesheet. Separating the document's content and the document's styling information allows displaying the same document on different media (like screen, paper, cell phone), and it also enables users to view the document according to their preferences and abilities, just by modifying the style sheet.

The Stylesheet

Here are two templates from the stylesheet used to format the XML file. The full stylesheet (which includes extra information on pagination and margins) is available.
...
<xsl:template match="FX">
  <fo:block font-weight="bold">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="speech[@speaker='Arthur']">
  <fo:block background-color="blue">
    <xsl:value-of select="@speaker"/>:
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>
...
The stylesheet can be used to transform any instance of the DTD it was designed for. The first rule says that an FX element will be transformed into a block with a bold font. <xsl:apply-templates/> is a recursive call to the template rules for the contents of the current element. The second template applies to all speech elements that have the speaker attribute set to Arthur, and formats them as blue blocks within which the value speaker attribute is added before the text.

Thanks http://www.w3.org