Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced XSLT II. 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.

Similar presentations


Presentation on theme: "Advanced XSLT II. 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."— Presentation transcript:

1 Advanced XSLT II

2 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

3 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

4 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

5 XML Tutorial XML and DTDs XPath and XSLT The Data

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

7 Section.. </xsl:template

8 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!

9 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

10 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

11 Alan Turing computer scientist mathematician cryptographer The Data

12 Richard P Feynman physicist playing the bongoes The Data

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

14 <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform > Famous Scientists <xsl:apply-templates select = person mode = toc />

15 ,

16 Name: Born: Died:

17 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} />

18 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

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

20 , 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

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

22 150 …

23 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

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

25 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

26

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

28 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

29 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()

30 Fridge Contents XML XSLT

31 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 II. 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."

Similar presentations


Ads by Google