Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.

Similar presentations


Presentation on theme: "CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of."— Presentation transcript:

1 CH 15 XSL Transformations 1

2 Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of Processing multiple elements with xsl:foreach Matching and selecting nodes with patterns Understanding the default template rules 2

3 Objectives cont.. Attribute value templates Deciding which output to include Counting nodes Sorting output Modes Output methods 3

4 What Is XSL? XSL stand for Extensible Stylesheet Language It include both transformation language and a formatting language.  The Transformation - provides elements that define rules for how one XML document is transformed into another XML document  Formatting - ????? 4

5 Overview of XSL Transformations In an XSL transformation, an XSLT processor reads both an XML document and an XSLT style sheet XSLT is designed primarily for XML-to-XML and XML-to-HTML transformations root  Trees - A tree is a data structure composed of connected nodes beginning with a top node called the root 5

6 Overview of XSL Transformations cont… One useful property of a tree is that each node and its children also form a tree XSLT processors model an XML document as a tree that contains seven kinds of nodes ✦ The root ✦ Elements ✦ Text ✦ Attributes ✦ Namespaces ✦ Processing instructions ✦ Comments 6

7 Overview of XSL Transformations cont… Unlike CSS, XSL is not limited to working only with whole elements XSLT operates by transforming one XML tree into another XML tree XSLT cannot transform from non-XML formats such as PDF, TeX, Microsoft Word, PostScript, MIDI, or others XSLT can work with HTML XSLT is not a general-purpose language for transforming arbitrary data 7

8 Overview of XSL Transformations cont…  XSLT style sheet documents template rules An XSLT document contains template rules A template rule has a pattern specifying the nodes it matches A template to be instantiated and output when the pattern is matched XSLT processor transforms an XML document under the control of an XSLT style sheet, it walks the XML document tree starting at the root, and following an order defined by the template rules 8

9 Overview of XSL Transformations cont… xsl:template Each template rule is an xsl:template element match xsl:template The pattern of the rule is placed in the match attribute of the xsl:template element All instructions in the template for doing things, such as selecting parts of the input tree to include in the output tree, are performed by XSLT elements 9

10 Overview of XSL Transformations cont… 10

11 Overview of XSL Transformations cont… Where does the XML transformation happen? 1. The XML document and associated style sheet are both served to the client(web browser), which then transforms the document as specified by the style sheet and presents it to the user 2. The server applies an XSLT style sheet to an XML document to transform it to some other format (generally HTML) and sends the transformed document to the client (web browser) 3. A third program transforms the original XML document into some other format(often HTML) before the document is placed on the server. Both server and client only deal with the transformed document 11

12 XSL templates xsl:template Template rules defined by xsl:template elements are the most important part of an XSLT style sheet This example below match the root node of the input tree 12

13 XSL templates cont… xsl:apply-templates The xsl:apply-templates element An xsl:apply-templates element tells the processor to compare each child node of the matched source element against the templates in the style sheet If a match is found, output the template for the matched node Review page 436 13

14 XSL templates  The select attribute To choose a particular set of children instead of all children, supply xsl:apply-templates with a select attribute designating the children to be selected, as in this template rule Example http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdc atalog&xsltfile=cdcatalog_apply 14

15 XSL templates The select attribute uses the same kind of patterns as the match attribute of the xsl:template element If no select attribute is present, all child element, text, comment, and processing instruction nodes are selected. 15

16 Computing the Value of a Node with xsl:value-of xsl:value-of The xsl:value-of element computes the string value of something Example 16

17 Processing Multiple Elements with xsl:for-each There are two ways of processing multiple elements in turn First method xsl:apply-templatesselect Simple use xsl:apply-templates with a select attribute The select=”.” in the second template tells the formatter to take the value of the matched node, ATOM 17 OR

18 Processing Multiple Elements with xsl:for-each xsl:for-each Second method is xsl:for-each xsl:for-each element processes each element chosen by its select attribute in turn This is useful when you need to format the same content differently in different places in the style sheet 18

19 Patterns for Matching Nodes The match attribute of the xsl:template element supports a complex syntax that allows you to indicate precisely which nodes you do and do not want to match  Matching the root node XSLT style sheets generally start with a rule that applies to the root node 19

20 Patterns for Matching Nodes This rule applies to the root node and only the root node of the input tree  Matching element names This template matches ATOM elements and makes their ATOMIC_NUMBER children bold 20

21 Patterns for Matching Nodes  Wildcards (*) Sometimes you want a single template to apply to more than one element Use (*) to indicate that a template matches all elements This above template says that all input elements should be wrapped in a P element 21 xsl:template match=”*”> xsl:template match=”*”>

22 Patterns for Matching Nodes  Matching children with / You can use the / symbol to match hierarchies of elements / symbol root Alone, the / symbol refers to the root node Between two names it indicates that the second is the child of the first E.g. / ATOM/NAME refers to NAME elements that are children of ATOM elements 22

23 Patterns for Matching Nodes This rule selects SYMBOL elements that are children of ATOM elements. Read it backward. The. in refers to the SYMBOL and not to the ATOM 23

24 Patterns for Matching Nodes You can specify deeper matches by stringing patterns together. E.g. PERIODIC_TABLE/ATOM/NAME PERIODIC_TABLE/ATOM/NAME selects NAME elements whose parent is an ATOM element whose parent is a PERIODIC_TABLE element * You can also use the * wildcard to substitute for an arbitrary element name in a hierarchy 24

25 Patterns for Matching Nodes This template rule applies to all SYMBOL elements that are grandchildren of a PERIODIC_TABLE element 25

26 Patterns for Matching Nodes  Matching descendants with // Bypass intermediate nodes and simply select all the elements of a given type This template rule applies to all NAME descendants of PERIODIC_TABLE, no matter how deep 26 // //

27 Patterns for Matching Nodes  Matching by ID You might want to apply a particular style to a particular single element without changing all other elements of that type 27

28 Patterns for Matching Nodes  Matching attributes with @ see Listing 15-7 for example This template above rule matches UNITS attributes, and wraps them in an I element 28

29 Attribute Value Templates Attribute value templates enable a style sheet to determine the content of an attribute dynamically based on the content of the input document rather than using a literal fixed value in the style sheet 29

30 Attribute Value Templates cont… How would you extract the content of the elements and the value of the attributes? 30 ” ATOMIC_WEIGHT=” ” ATOMIC_NUMBER=” ” /> ” ATOMIC_WEIGHT=” ” ATOMIC_NUMBER=” ” /> Wrong !!! The < character is not allowed in an attribute value

31 Attribute Value Templates cont… curly braces {} Instead, inside attribute values, data enclosed in curly braces {}, takes the place of the xsl:value-of element 31 ATOMIC_WEIGHT=”{ATOMIC_WEIGHT}” ATOMIC_NUMBER=”{ATOMIC_NUMBER}” /> The correct way to write the preceding template rule is like this

32 Attribute Value Templates cont… In the previous slide, In the output, {NAME} is replaced by the value of the NAME child element of the matched ATOM {ATOMIC_WEIGHT} is replaced by the value of the ATOMIC_WEIGHT child element of the matched ATOM {ATOMIC_NUMBER} is replaced by the value of the ATOMIC_NUMBER child element 32

33 Counting Nodes with xsl:number The xsl:number element inserts a formatted integer into the output document The value of the integer is given by the value attribute See Listing 15-17. 33

34 Counting Nodes with xsl:number cont… Number to string conversion xsl:number: These are all adjustable through four attributes of xsl:number: ✦ format ✦ letter-value ✦ grouping-separator ✦ grouping-size 34

35 Counting Nodes with xsl:number cont… xsl:number The format attribute - You can adjust the numbering style used by xsl:number using the format attribute ✦ i—The lowercase Roman numerals i, ii, iii, iv, v, vi,... ✦ I—The uppercase Roman numerals I, II, III, IV, V, VI,... ✦ a—The lowercase letters a, b, c, d, e, f,... ✦ A—The uppercase letters A, B, C, D, E, F,... 35

36 Counting Nodes with xsl:number cont… Using xsl:format with number This rule numbers the atoms with capital Roman numerals You can specify decimal numbering with leading zeros by including the number of leading zeros you want in the format attribute. E.g. format=“01”. This will produces the sequence 01,02,03 36

37 Counting Nodes with xsl:number cont… Using letter-value with xsl:number The letter-value attribute distinguishes between letters interpreted as numbers and letters interpreted as letters. E.g. 37 For instance, if you want to use format=”I” to start the sequence I, J, K, L, M, N,... instead of I, II, III, IV, V, VI,..., you would set the letter-value attribute to the keyword alphabetic

38 Sorting Output The xsl:sort element sorts the output nodes into a different order than they were generated in xsl:sort element appears as a child of an xsl:apply-templates element or xsl:for-each element See Listing 15-18 38 Order of the sort from the default ascending order to descending by setting the order attribute to descending

39 Making Choices XSLT provides two elements that allow you to choose different output based on the input The xsl:if element either does or does not output a given fragment of XML depending on what patterns are present in the input The xsl:choose element 39

40 Making Choices cont… The xsl:if element provides a simple facility for changing the output The test attribute of xsl:if contains an expression that evaluates to a boolean There are no xsl:else or xsl:else-if elements 40,,

41 Making Choices cont… xsl:choose The xsl:choose element selects one of several possible outputs depending on several possible conditions xsl:when Each condition and its associated output template is provided by an xsl:when child element 41

42 Making Choices cont… 42 This rule changes the color of the output based on whether the STATE attribute of the ATOM element is SOLID, LIQUID, or GAS

43 Output  Document Type Declaration XSLT does not provide any elements for building a DTD for the output document with,,, and declarations 43

44 Output  CDATA sections XSLT does not allow you to insert CDATA sections at arbitrary locations in XML documents produced by XSL transformations 44

45 references XSL Tutorial http://www.w3schools.com/xsl/default.asp 45


Download ppt "CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of."

Similar presentations


Ads by Google