+ All Categories
Home > Documents > TUTORIAL 7

TUTORIAL 7

Date post: 31-Dec-2015
Category:
Upload: tashya-hudson
View: 15 times
Download: 0 times
Share this document with a friend
Description:
TUTORIAL 7. CREATING A COMPUTATIONAL STYLESHEET (Using XSLT style sheet to calculate values based on data from the source document). OBJECTIVES. In this chapter you will: Learn how to number nodes Apply XPath functions such as count() and sum() Create formulas using mathematical operators - PowerPoint PPT Presentation
49
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET (Using XSLT style sheet to calculate values based on data from the source document)
Transcript
Page 1: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

1

TUTORIAL 7

CREATING A COMPUTATIONAL STYLESHEET

(Using XSLT style sheet to calculate values based on data from the source document)

Page 2: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

2

OBJECTIVES

In this chapter you will:• Learn how to number nodes• Apply XPath functions such as count() and sum()• Create formulas using mathematical operators• Work with text nodes and white space• Create variables and parameters

Page 3: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

3

OBJECTIVES

In this chapter you will:• Create named and recursive templates• Work with multiple style sheets• Learn how to use extension functions and

elements

Page 4: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

4

NUMBERING NODES

• Goal: add numbers to the items in columns

• Number nodes using:– Xpath’s position() function

• Position of the element in the result document

• Can be used in conditional statements

<xsl:if test=“position()=3 />

– XSLT <xsl:number> element<xsl:number select=“expression” />

Page 5: TUTORIAL 7

XPUsing the number Element

Source document<products>

<item>Goblin Fountain</item>

<item>Tracer Fire</item>

<item>Dragon Fountain</item>

<item>Rock the Sky</item>

<item>Pyro Blast</item>

</products>

Style<xsl:for-each select=“products/item”>

<xsl:number />

<xsl:value-of select=“.” />

<br/>

</xsl:for-each>

New Perspectives on XML, 2nd EditionTutorial 7

5

Numbered list1. Goblin Fountain2. Tracer Fire3. Dragon Fountain4. Rock the Sky5. Pyro Blast

Page 6: TUTORIAL 7

XPCounting subset of items

Source document<products>

<fountain>Goblin Fountain</fountain>

<rocket>Tracer Fire</rocket>

<fountain>Dragon Fountain</fountain>

<rocket>Rock the Sky</rocket>

<rocket>Pyro Blast</rocket>

</products>

Style<xsl:for-each select=“fountain”>

<xsl:number />

<xsl:value-of select=“.” />

<br/>

</xsl:for-each>

New Perspectives on XML, 2nd EditionTutorial 7

6

Numbered list1. Goblin Fountain2. Dragon Fountain

Page 7: TUTORIAL 7

XPLevels

Source document<products>

<fountains>

<item>Goblin Fountain</item>

<item>Dragon Fountain</item>

</fountains>

<rockets>

<item>Tracer Fire</item>

<item>Rock the Sky</item>

<item>Pyro Blast</item>

</rockets>

</products>

Style<xsl:for-each select=“//item”>

<xsl:number />

<xsl:value-of select=“.” />

<br/>

</xsl:for-each>

New Perspectives on XML, 2nd EditionTutorial 7

7

Numbered list1. Goblin Fountain2. Dragon Fountain1. Tracer Fire2. Rock the Sky3. Pyro Blast

Page 8: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

8

USING <XSL:NUMBER>

• Nodes are numbered according to position in source document

• Attributes:– select=expression: any XPath expression that evaluates to a

number

– count=pattern: specifies which nodes to count

– level=type: tree level for nodes to count• any = across different levels as if they were all siblings

• single = items at one level only (defaut)

• multiple = hierarchy count

Page 9: TUTORIAL 7

XPHierarchical numbered list

Source document<products>

<group>

<item>Goblin Fountain</item>

<item>Dragon Fountain</item>

</group>

<group>

<item>Tracer Fire</item>

<item>Rock the Sky</item>

<item>Pyro Blast</item>

</group>

</products>

Style<xsl:for-each select=“//group |

//item”>

<xsl:number level=“multiple” count=“group | item” /> />

<xsl:vale-of select=“.” />

<br/>

</xsl:for-each>

New Perspectives on XML, 2nd EditionTutorial 7

9

Hierarchical list1. Fountains1.2. Goblin Fountain1.2. Dragon Fountain2. Rockets2.1. Tracer Fire2. 2. Rock the Sky2. 3. Pyro Blast1

Page 10: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

10

USING <XSL:NUMBER>

• Attributes:– from=pattern: pattern indicates where numbering should restart

– format=pattern: pattern indicates number format• 1=integer (1, 2, 3,…)

• A= alphabetical uppercase (A, B, C, …)

• a= alphabetical lowercase (a, b, c, …)

• i= lowercase Roman numerial (i, ii, iii, iv, v, …)

• I= uppercase Roman numerial (I, II, III IV, V, …)

– grouping-size, grouping-separator: indicate how digits are grouped and separator character. For example

<xsl:number format=“1” grouping-size=“2” grouping-separator=“ ” />

displays “548710” as “54 87 10”

Page 11: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

11

WORKING WITH XPATH FUNCTIONS

• Used to calculate numerical values or manipulate text strings

• Numerical functions:

Examples: <xsl: value-of select=“count(//customer)” /> <xsl: value-of select=“//@sum(qty)” />

Page 12: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

12

XPATH TEXT FUNCTIONS

Page 13: TUTORIAL 7

XPXPATH STRING FUNCTION Example

• Substring-before (“C101 C102 C103 C104”, “ ”)• Locates the first blank space in the text string and

extracts all the text before the first blank, e.g., “C101”

New Perspectives on XML, 2nd EditionTutorial 7

13

Page 14: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

14

WORKING WITH MATHEMATICAL OPERATORS

• Six operators:

Example: <xsl:value-of select=“@qty * @price” />

Page 15: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

15

FORMATTING NUMBERS

• XPath function format-number• Syntax: format-number (value, format)• Example: format-number (56823.847, “#,##0.00”)

displays 56,823.85• Another example: format-number (56823.847,

“#.##0,00”) displays 56.823,85

Page 16: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

16

NUMBER FORMAT SYMBOLS

Page 17: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

17

<XSL:DECIMAL-FORMAT>

• Format: <xsl:decimal-format attributes />

• Example:<xsl:decimal-format name=“Europe” decimal-separator=“,”

grouping separator=“.” />

Page 18: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

18

<XSL:DECIMAL-FORMAT> ATTRIUTES

Page 19: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

19

WORKING WITH TEXT NODES AND WHITE SPACE

• White space:– Space (press space bar), &#x20;

– Tab (pressing the tab key) &#x9;

– new line (pressing Enter key) &#xA;

– carriage return &#xD;

– nonbreaking space &#160;

• Adjacent <xsl:value-of> elements will have no white space between them

• <xsl:text> can be used to create white space:– Syntax: <xsl:text>&#160</xsl:text>

Page 20: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

20

CONTROLLING WHITESPACE

• Stripping space:– Remove text nodes that contain only white space from

the result document

– Syntax: <xsl:strip-space elements=“list”>

– where list is a white-space separated list of elements in the source document that contain white space characters

– Use * match all nodes

Page 21: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

21

CONTROLLING WHITESPACE

• Preserving space:– Make sure that text nodes that contain only white space are

not deleted– Syntax: <xsl:preserve-space elements=“list”>– Use * as pattern to match all nodes

• Normalize space:– Remove leading and trailing spaces– X-pathSyntax: normalize-space(text)– Example: normalize-space (“ goodbye ”)

returns the text string “goodbye”

Page 22: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

22

USING VARIABLES

• User-defined name that stores a particular value or object, such as:

• Types:– number

– text string

– node set

– boolean value (either true or false)

– result tree fragment

Page 23: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

23

VARIABLE SCOPE

• Global:– Can be referenced from anywhere within the style sheet

– Must be declared at the top level of the style sheet, as a direct child of the <xsl:stylesheet> element

– Must have a unique variable name

• Local:– Referenced only within template

– Can share name with other local or global variable

Page 24: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

24

USING VARIABLES

• Declaring variables– Syntax: <xsl:variable name=“name” select=“value” />

– Example: <xsl:variable name=“Months” select=“12” />

• Names are case-sensitive

• Value only set once upon declaration

• Enclose text strings in single-quotes

• Example<xsl:variable name=“company” select=“‘Wizard Works’” />

• Referencing a variable:The name of the company is <xsl:value-of select=“$company” />

Note use of $

Not like variables of other languages

Page 25: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

25

COPYING

• <xsl:copy>– Syntax: <xsl:copy use-attribute-sets=“list” />

– Shallow copy: only node itself is copied

• <xsl:copy-of>– Syntax: <xsl:copy-of select=“expression”/>

– Deep copy: node and descendants are copied

– Example:<tr>

<td><xsl:value-of select=“@date” /> </td>

<tr>

Page 26: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

26

USING PARAMETERS

• Similar to variables, but:– Value can be changed after it is declared

– Can be set outside of scope

• Syntax: <xsl:param name=“name” select=“value”/>

• Example: <xsl:param name=”Filter” select=”'C103'” />

• To reference: $param-name

Page 27: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

27

SETTING PARAMETER VALUES EXTERNALLY

• Depends on XSLT processor– Some work by appending parameter value to url

• Web browsers to not allow users to set parameters values directly– Use a JavaScript program from within the browser (see

Tutorial 10)

Page 28: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

28

TEMPLATE PARAMETERS

• Local in scope• Created inside <xsl:template> element• To pass parameter to template

– place <xsl:with-param> element in <xsl:apply-templates> element

– Syntax: <xsl:with-param name=“name” select=“value”/>

– No error if calling param name does not match template param name

Page 29: TUTORIAL 7

XPTEMPLATE PARAMETERS

• Example

<xsl:apply-templates select=“customers/customer”>

<xsl:with-param names=“custID” select=“C101” />

</xsl:apply-templates>

<xsl:template match=“customer”>

<xsl:parm name=“custID”/>

styles

</xsl:tmplate>

New Perspectives on XML, 2nd EditionTutorial 7

29

Page 30: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

30

INTRODUCING FUNCTIONAL PROGRAMMING

• Functional programming language:– Relies on the evaluation of functions and expressions,

not sequential execution of commands

– Different from most other languages

Page 31: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

31

FUNCTIONAL PROGRAMMING PRINCIPLES

• Main program consists entirely of functions with well-defined inputs

• Results of program are defined in terms of function outputs

• No assignment statements; when a variable is declared, its value cannot be changed

• Order of the execution is irrelevant

Page 32: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

32

FUNCTIONAL PROGRAMMING

• Easier to maintain and less susceptible to error• No order of execution• Each time a function is called, it does the same

thing, regardless of how many times it has already been called, or the condition of other variables in the program

• Important to think about the desired end result, rather than the sequential steps needed to achieve that effect

Page 33: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

33

RECURSION

• In functional programming, recursion takes the place of looping structures

• Function calls itself

Page 34: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

34

USING NAMED TEMPLATES

• Template not associated with node set• Collection of functions and commands that are

accessed from other templates in the style sheet• Syntax:

<xsl:template name="name">

styles, variables and parameters

</xsl:template>

Page 35: TUTORIAL 7

XPUSING NAMED TEMPLATES

• Example<xsl:template name=“totalCost”/>

<xsl:param name=“list” />

<xsl: parm name=“total” select = “0” />

<!– Calculate the total item cost />

</sxl:template>

New Perspectives on XML, 2nd EditionTutorial 7

35

Page 36: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

36

CALLING NAMED TEMPLATES

• Syntax:

<xsl:call-template name=“name”>

<xsl:with-param />

<xsl:with-param />

...

</xsl:call-template>

Page 37: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

37

WRITING A RECURSIVE TEMPLATE

• Templates that call themselves, usually passing along a new parameter value with each call

• Needs to have a stopping condition– Expressed in an if statement or a choose statement

– If missing, will call itself without end

Page 38: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

38

WRITING A RECURSIVE TEMPLATE

• Syntax with <xsl:if>:

Page 39: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

39

WRITING A RECURSIVE TEMPLATE

• Syntax with <xsl:choose>

Page 40: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

40

WORKING WITH MULTIPLE STYLE SHEETS

• Use <xsl:include> or <xsl:import>– Not supported by all browsers

• Use to create a library of XSLT code• <xsl:include>

– Syntax: <xsl:include href=“URL” />– Same as inserting the components of included sheet

directly into including file– Does not perform a direct text copy

• Does not copy opening and closing <xsl:stylesheet> tags

• Copies the elements and templates within the style sheet

Page 41: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

41

WORKING WITH MULTIPLE STYLE SHEETS

• <xsl:import>– Syntax: <xsl:import href=”URL” />

– Must be at the top level of the style sheet; must be first child of <xsl:stylesheet>

– If name conflicts occur, importing sheet takes precedence

– If you import several style sheets, the last one imported has the highest precedence among the imported sheets in the event of a name conflict.

Page 42: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

42

WORKING WITH EXTENSION FUNCTIONS

• Provide extended functionality specific to XSLT processor– Caution: use extensions only when you are sure that a given

document is always acessed using the same processor.

• Extension functions - extend the list of functions available to XPath and XSLT expressions

• Extension elements - extend the list of elements that can be used in an XSLT style sheet

• Extension attributes - extend the list of attributes associated with XSLT elements

• Extension attribute values - extend the data types associated with XSLT attributes

Page 43: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

43

WORKING WITH EXTENSION FUNCTIONS

• Check processor documentation for support

• EXSLT:

– Proposed common set of extensions

– Not fully supported by all processors

– Some functions supported by:• 4XSLT

• saxon

• jd.xslt

• libxslt

• Xalan-J

Because extension functions are not available on all XSLT processors, just be aware of them. They will not be on the final exam

Page 44: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

44

USING EXTENSIONS

• Modify <xsl:stylesheet> to include:<xsl:stylesheet version=”1.0”

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”

xmlns:math=”http://www.exslt.org/math”

extension-element-prefixes=”math”>

• Syntax: prefix:function()• Test function availability: function-available(“extension”)

Page 45: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

45

EXSLT MATH FUNCTIONS

Page 46: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

46

WRITING EXTENSION FUNCTIONS (MSXML)

• Add:<msxsl:script language=“language” implements-prefix=“refix”>

script commands

</msxsl:script>

where language is the language of the script and

prefix is the namespace prefix you want to assign to the extension functions

script commands are the commands needed to create the extension function

Processor used by IE

Page 47: TUTORIAL 7

XPExample: random()

<xsl:stylesheet version=“1.0”

xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

xmlns:jsext:=“http://javacrript-extensions”

xmlns:msxsl=“urn:schema-microsoft-com:xslt”

extension-element-prefixes=“jsext masxsl”>

<maxsl:script implements-prefix=“jsext” language = “javscript”>

function random() {

returns Math.random();

} </maxsl:script>

<xsl:value-of select=“jsext:random()” />

New Perspectives on XML, 2nd EditionTutorial 7

47

Works only with MSXML

Page 48: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

48

SUMMARY

• <xsl:number> and position() are used to number elements

• XPath provides mathematical and string operators• Standard mathematical operators (+, -, etc.) can be

used in XSLT• Number formatting uses format-number() function

Page 49: TUTORIAL 7

XP

New Perspectives on XML, 2nd EditionTutorial 7

49

SUMMARY

• White space characters are controlled with various XSLT elements and XPath functions

• Variables and parameters are used to supply user-defined values

• XSLT is a functional programming language• Recursive templates provide looping logic in

XSLT• EXSLT provides a set of extension functions,

elements, attributes and attribute values


Recommended