Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.

Similar presentations


Presentation on theme: "XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University."— Presentation transcript:

1 XSLT Kanda Runapongsa (krunapon@kku.ac.th)krunapon@kku.ac.th Dept. of Computer Engineering Khon Kaen University

2 168493: XML and Web Services (II/2546) 2 XSL - The Style Sheet of XML XML does not use predefined tags (we can use any tags we want) could mean an HTML table, a piece of furniture, or something else XSL: something in addition to the XML document that describes how the document should be displayed

3 168493: XML and Web Services (II/2546) 3 What is XSLT? XSLT transforms an XML document into another XML document, such as an XHTML document XSLT can Add new elements into the output file Remove elements Rearrange and sort elements Test and make decisions about which elements to display

4 168493: XML and Web Services (II/2546) 4 How Does XSLT Work? XSLT transforms an XML source tree into an XML result tree XSLT uses XPath to define parts of the source document that match one or more predefined templates

5 168493: XML and Web Services (II/2546) 5 How Does XSLT Work? When a match is found, XSLT will transform the matching part of the source document into the result document The parts of the source document that do not match a template will end up unmodified in the result document

6 168493: XML and Web Services (II/2546) 6 Style Sheet Declaration The root element that declares the document to be an XSL sheet is or The correct way to declare an XSL style sheet according to the W3C XSL Recommendation is: <xsl:stylesheet version=“1.0”? xmlns:xsl=“http://www.w3.org/1999/XSL/Tr ansform”>http://www.w3.org/1999/XSL/Tr ansform

7 168493: XML and Web Services (II/2546) 7 XSL Uses Templates An XSL style sheet consists of a set of rules called templates Each element contains rules to apply when a specified node is matched The match attribute is used to associate the template with a set of XML elements

8 168493: XML and Web Services (II/2546) 8 XSL Uses Templates (Cont.) The match attribute can also be used to define a template for a whole branch of the XML document Match = “/” defines the whole document What is another way to select the entire document? An XSL processor parses an XML source and tries to find a matching template rule. If it does, instructions inside matching template are evaluated

9 168493: XML and Web Services (II/2546) 9 Sample XSL File <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/ XSL/Transform"> My CD Collection Title Artist

10 168493: XML and Web Services (II/2546) 10 The first lines of the XSL file Since the style sheet is an XML document itself, the document begins with an XML declaration The tag defines the start of the style sheet Every XSL file needs to specify the XSL namespace so that the parser knows which version of XSLT to use

11 168493: XML and Web Services (II/2546) 11 Namespace in XSL The namespace prefix xsl: is used in the rest of the XSL file to identify XSL processing statements If a statement is not prefixed with xsl:, then it’s simply copied to the output without being processed. This is the way to add HTML statements to the output

12 168493: XML and Web Services (II/2546) 12 The Element Before processing can begin, the part of the XML document with the transformation must be selected with an XPath expression The selected section of the document is called a node and is normally selected with the match operator

13 168493: XML and Web Services (II/2546) 13 The Element The element extracts the value of a selected node Example: Result: Hide your heart

14 168493: XML and Web Services (II/2546) 14 The Element The element allows you to do looping in XSL It selects every XML element of a specified node set Selects every ‘catalog/cd’ element

15 168493: XML and Web Services (II/2546) 15 Filtering the Output We can filter the output from an XML file by adding a criterion to the select attribute in the element

16 168493: XML and Web Services (II/2546) 16 The Element The element is used to sort the output To output the XML file as an XHTML file, and sort it at the same time, simply add a sort element inside the for-each element in the XSL file

17 168493: XML and Web Services (II/2546) 17 The Element The select attribute indicates what XML elements to sort on

18 168493: XML and Web Services (II/2546) 18 The Element The element contains a template that will be applied only if a specified condition is true some output … The value of the required test attribute contains the expression to be evaluated

19 168493: XML and Web Services (II/2546) 19 The Element The element is used in conjunction with and to express multiple conditional tests If no element is true, and no element is present, nothing is created

20 168493: XML and Web Services (II/2546) 20 The Element

21 168493: XML and Web Services (II/2546) 21 The Element The element applies a template rule to the current element or to the current element’s child nodes The element is always found inside a template body

22 168493: XML and Web Services (II/2546) 22 (Cont.)

23 168493: XML and Web Services (II/2546) 23 The : An instruction to apply templates to the children of the current nodes My CD Collection …

24 168493: XML and Web Services (II/2546) 24 The Element The element allows an element to be created with a computed name It generates elements in time of processing 168493

25 168493: XML and Web Services (II/2546) 25 The Element The element can be used to add attributes to result elements whether created by Literal result elements in the style or Instructions such as xsl:element center

26 168493: XML and Web Services (II/2546) 26 The Element The element provides an easy way of copying the current node The namespace nodes of the current node are automatically copied It may have a use-attribute-set attribute to specify and also copy attributes for copied element

27 168493: XML and Web Services (II/2546) 27 (Cont.) Example Greeting

28 168493: XML and Web Services (II/2546) 28 The element The xsl:copy-of element can be used to insert a result tree fragment into the result tree, without first converting it to string as xsl:value-of does But when the result is neither a node-set nor a result tree fragment, the result is converted to a string and then inserted into the result tree

29 168493: XML and Web Services (II/2546) 29 The Element XML: Hello Output: Hello

30 168493: XML and Web Services (II/2546) 30 The Element The element allows stylesheet authors to specify how they wish the result tree to be output If an XSLT processor outputs the result tree, it should do so as specified by the element However, it is not required to do so

31 168493: XML and Web Services (II/2546) 31 The Element xsl:stylesheetversionxsl:outputmethodxsl:templatematchxsl:copy-ofselectxsl:templatexsl:stylesheet

32 168493: XML and Web Services (II/2546) 32 The Element XML source Output (in HTML)

33 168493: XML and Web Services (II/2546) 33 The Element xsl:stylesheetversionxsl:outputmethodxsl:templatematchxsl:copy-ofselectxsl:templatexsl:stylesheet

34 168493: XML and Web Services (II/2546) 34 The Element XML source Output (in XML)

35 168493: XML and Web Services (II/2546) 35 The Element The element is used to insert a formatted number into the result tree The number to be inserted may be specified by an expression The value attribute contains an expression If no value attribute is specified, the xsl:number element inserts a number based on the position of the current node

36 168493: XML and Web Services (II/2546) 36 (Cont.) Output: 250.000 <xsl:number value=“250000” grouping-size=“2”/> Output: 25,00,00

37 168493: XML and Web Services (II/2546) 37 The Element Literal data characters may be wrapped in an element This wrapping may change what whitespace characters are stripped but does not affect how the characters are handled by the XSLT processor

38 168493: XML and Web Services (II/2546) 38 The Element XML source Output

39 168493: XML and Web Services (II/2546) 39 The Element The specify a variable which is a name that may be bound to a value The value to which a variable is bound (the value of the variable) can be an object of any of the types that can be returned by expressions

40 168493: XML and Web Services (II/2546) 40 The Element Both and are used to bind variables But the value specified on the variable is only a default value for the binding The value specified on the can be changed later


Download ppt "XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University."

Similar presentations


Ads by Google