Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.

Similar presentations

Presentation on theme: "Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another."— Presentation transcript:

1 Advanced XSLT

2 Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another –The function is written in XML The transform flow can branch depending on the content Similar idea to an if statement in OOP

3 The if element allows the transform to branch It contains further processing instructions Branching is controlled by a Boolean attribute called test if test has value true, the transform branches through the content of if

4 Boolean Values test is an XML attribute of if –its value is an XPath expression –this will be interpreted as a Boolean standard rules apply to translate the results of XPath expressions to Booleans XPath typically evaluates to the following types: –number, node set, string

5 translation to Boolean values Number +0 or -0 or NaN false, all other numbers true node-set –empty node-set false, non-empty node-set true String –empty string false, all other strings true true() and false() functions

6 True or false? - always true - returns false - true if and only if non-empty node-set - depends on number of zones examples

7 example Some XML data contains zone elements If the number of zone elements is greater than 2, we wish to produce some HTML A template for zone has been predefined We will use if to decide whether to transform the zone elements into HTML

8 <!the following instructions are processed if and only if the number of zone elements in the current context is greater than 2 --> Applicable Zones example

9 Example - notes We have a separate template for zone This can be developed separately from the if transform An XSLT can be built up from different parts Use templates to divide your transform into easy-to-program steps

10 Branching in XSLT Several nested if elements become difficult to program The choose element plays the role of a case statement in XSLT The transform will branch into one of a choice of transforms depending on the value of an XPath expression

11 contains one or more elements –when operates exactly like if –test attribute with Boolean XPath value –content is further transform instructions choose may contain one optional otherwise element –represents the default case –content is further transform instructions

12 example we will define an attribute value using choose the attribute will be a bgcolor for table rows we want the rows to alternate between orange and green we use the position() function in an XPath expression for our test

13 orange green

14 example - notes the attribute element can be used to define an attribute during the transform –can be inserted in output – see later the text element is used to precisely define literal text content the example again relies on pre-defined templates for other elements (the content of zone)

15 Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined by an XPath expression we use a for-each element to control the iteration

16 Applies processing to all nodes in a given node set select attribute contains XPath expression defining the node set the current node changes at each iteration of for-each –changes the context for transforms of content

17 example we have XML data for a tutorial, divided into sections we wish to produce a numbered contents list in HTML we use for-each twice to create a numbered heading and a list of sections we use the position() function for the numbering

18 XML Tutorial XML and DTDs XPath and XSLT The Data

19 Section 1. XML Tutorial 1. XML and DTDs 2. XPath and XSLT … and so on for each section The Desired Output

20 Section.. </xsl:template

21 example - notes for-each select=section selects all section nodes in the current tutorial –position() will number the sections in document order for-each select=panel selects all panel nodes in the current section this is just a nested loop!

22 modes Sometimes you may wish to transform the same node in more than one way The transform for a node is defined in a template A mode attribute is available for template and apply-templates elements This allows more than one template to be defined for any node

23 example XML data for a famous scientists website The data contains a name element for each person featured on the site. We use mode to achieve two different presentations of name –table of contents –personal data entry

24 Alan Turing computer scientist mathematician cryptographer The Data

25 Richard P Feynman physicist playing the bongoes The Data

26 The Desired Output Turing, Alan Feynman, Richard Name: Alan Turing Born: 1912 Died: 1954 Professions: computer scientist, mathematician, cryptographer Hobbies: none known

27 <xsl:stylesheet version = 1.0 xmlns:xsl= > Famous Scientists <xsl:apply-templates select = person mode = toc />

28 ,

29 Name: Born: Died:

30 attribute value templates allows attribute values derived from the source to be set in the output example <name first = {first_name} initial = {middle_initial} last = {last_name} />

31 specifies a particular template to be invoked perhaps not related directly to a node-set –e.g. to add boilerplate start_html markup template must have a name attribute calls the template by name

32 <!Interesting stuff that generates the masthead goes in here --> … some-title

33 , defines parameters for templates parameters can be passed to templates from either call-template or apply-template elements containing elements name attribute names the parameter select attribute (or content) defines the value of the parameter

34 XSLT processing flow reference to template and parameter passed return to XSLT processing flow

35 150 …

36 allows values to be stored to assist with processing flow logic name attribute names the variable select attribute (or content) defines the value of the variable value can also be defined from element content

37 13 15 7, $y = 13 else $y = 15 -->

38 appears inside or inside sorts a node-set according to some criterion successive sort elements impose sub-orders select attribute defines the sorting criterion data-type defines the type of sort –text (the default) –number


40 more sort attributes order –ascending (default) –descending case-order –upper-first –lower-first

41 document ( ) converts a parameter into a URI –relative to current document –second parameter gives an alternative base URI returns a node-set associated with the URI allows content to be extracted from multiple XML documents without parameter defaults to current document

42 multiple XML files XSLT document XSLT processor output document set of template rules match elements and replace using template rules XML source XML source XML source XML source XML source XML source master document document()

43 Fridge Contents XML XSLT

44 XSLT extensions extension elements are passed to external code (extension functions) for processing –Java, Perl, C# etc. extends power of XSLT to arbitrary transformations implementation varies between XSLT processors –Xalan, Saxon, XT all allow extensions, but in different ways

Download ppt "Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another."

Similar presentations

Ads by Google