XSLT An Introduction 1 XSLT XSLT extensible Stylesheet
XSLT An Introduction 1
XSLT • XSLT (extensible Stylesheet Language: Transformations) is a language primarily designed for transforming the structure of an XML document. • XML as a simple, standard way to interchange structured textual data between computer programs, separates data from presentation and transmits data between applications. • Need to transform data to be usable by different data models of applications. • XSLT provides the transformation capability. 2
How does XSLT transform XML? • An XML Parser takes the source XML document and turns it into a tree representation. • The XSLT Processor, following the rules expressed in a stylesheet, transforms this tree into another tree. • A Serializer takes the result tree and turns it into a XML document. • Some well-known XSLT processors: • SAXON: is an open source XSLT processor developed by Michael Kay is a Java based application. It can be run from the command prompt. Instant Saxon is packaged for windows. http: //users. iclway. co. uk/mhkay/saxon 5 -5 -1/ • XALAN: from Apache is a Java-based application that can be run from a command prompt. 3
XSLT Transformation • When you invoke an XSLT processor to apply a particular source sheet to a particular source document, the following steps take place. – The source document is parsed an internal tree representation of the document is created in memory. – Then the transformation process starts. • The first step in the transformation is to find a template rule that matches the root node of the source tree. If no such rule is found, a built-in template is used. • The XSLT processor then instantiates the contents of this template rule. The content of the <xsl: template> element in the stylesheet is a sequence of elements and text nodes. • Instantiating a template body causes instantiation of 4 each of its components in the order they appear.
• Stylesheets contain a number of template rules. • Each template rule is expressed as an <xsl: template> element with a match attribute. • The value of the match attribute is a pattern – the pattern determines which nodes in the source tree the template rule matches. • Examples: – “/” matches a root node – “name” matches a name element – “person/name” matches a name element whose parent is a person element. 5
XSLT and SQL: An analogy • XSLT and SQL are both declarative. SQL uses declarative query using SELECT statements. XSLT used XPATH expressions to retrieve nodes from an XML document. • XPATH expressions retrieve nodes based on a path through the XML document. • XSLT is then used to manipulate the results of these queries (rearranging nodes, constructing new nodes etc). • Both languages exhibit closure – the output has same data structure as input, namely, a relation in SQL and a tree in XSLT. • Both languages augment the basic query facilities with useful additions for performing basic arithmetic, string manipulation and comparison operators. 6
XSLT and XPath • • XPath acts as a sublanguage within an XSLT stylesheet. An XPath expression is used to identify parts of the document to be processed. Used for numerical calculations, string manipulations or for testing boolean conditions. • Eg: <xsl: value-of select=“sum(//book/@price) div count(//book)”/> XSLT instruction XPath expression 7
XPath Queries • XPath is designed to process hierarchies and makes it easy to reference data by its position in the hierarchy. • Examples: – /shipping. Order/billing. Address/zipcode –. . /@title – /book/chapter[3]/section[2]/para[1] 8
Catalog. xml <? xml version="1. 0"? > <catalog> <book> <title language="English">Fun With XML</title> <author>John Robot</author> <isbn>12367</isbn> <price>70</price> </book> <title language="English">Xml and Java</title> <author>Mary Jones</author> <isbn>7856</isbn> <price>34</price> </book> 9
Example example. xsl <? xml version="1. 0"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/T ransform"> <xsl: template match="/"> <html> <xsl: apply-templates/> </html> </xsl: template> <xsl: template match="catalog"> <u>Catalog</u> </br> <body> <xsl: apply-templates/> </body> </xsl: template> <xsl: template match="book"> A book </br> </xsl: template> </xsl: stylesheet> From the command line in windowss, invoke saxon processor as follows: C: saxon. PathInstant_Saxonsaxon -o result. htm catalog. xml example. xsl Result. htm Catalog A book Tells the processor to process its children. 10
XSLT as a programming language • An XSLT stylesheet is an XML document and uses the tag structure of XML. • Similar to Perl and awk, processing in XSLT is expressed as a set of rules. • In XSLT, the rules are called template rules and are written in the form of <xsl: template> elements in the sylesheet. • Each template rule has – a match pattern that defines what kind of nodes in the tree it applies to. – A template body that defines what to do when the rule is fired. 11
The XPath Data Model • The tree structure used in XSLT and XPath is similar in many ways to the DOM with some important differences. • In DOM, every node has a node. Value property while in XPath, every node has a string-value property. • The node. Value of an element node in the DOM is null, while in XSLT and XPath, the string-value property of an element is the concatenation of all its descendent text nodes. 12
Example <xsl: template match="/"> <html> <!-- Process all the children --> <xsl: apply-templates/> </html> </xsl: template> <xsl: template match="catalog"> <body> <xsl: apply-templates/> </body> </xsl: template> <xsl: template match="book"> <p><u>Book: </u><xsl: value-of select="title"/> by <xsl: value-of select="author"/> </p> <br/> </xsl: template> </xsl: stylesheet> 13
- Slides: 13