Download presentation
Presentation is loading. Please wait.
Published byAileen Hunter Modified over 9 years ago
1
1 XSLT An Introduction
2
2 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.
3
3 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/saxon5-5-1/ XALAN: from Apache is a Java-based application that can be run from a command prompt.
4
4 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 and 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 element in the stylesheet is a sequence of elements and text nodes. Instantiating a template body causes instantiation of each of its components in the order they appear.
5
5 Stylesheets contain a number of template rules. Each template rule is expressed as an 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.
6
6 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.
7
7 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: XSLT instructionXPath expression
8
8 XPath Queries XPath is designed to process hierarchies and makes it easy to reference data by its position in the hierarchy. Examples: –/shippingOrder/billingAddress/zipcode –../@title../@title –/book/chapter[3]/section[2]/para[1]
9
9 Catalog.xml <title language="English">Fun With XML John Robot 12367 70 <title language="English">Xml and Java Mary Jones 7856 34
10
10 Example example.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL /Transform"> Catalog A book From the command line in windowss, invoke saxon processor as follows: C:\saxonPath\Instant_Saxon\saxon -o result.htm catalog.xml example.xsl Result.htm Catalog A book Tells the processor to process its children.
11
11 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 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.
12
12 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 nodeValue property while in XPath, every node has a string-value property. The nodeValue 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.
13
13 Example Book: by
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.