Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.

Similar presentations


Presentation on theme: "XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended."— Presentation transcript:

1 XML Language Family Detailed Examples Most information contained in these slide comes from: http://www.w3.org, http://www.zvon.org/ These slides are intended to be used as a tutorial on XML and related technologies Slide author: Jürgen Mangler (juergen.mangler@univie.ac.at) This section contains examples on: XSLT (eXtensible Stylesheet Language - Transformations)

2 With XSL you can freely modify the content and/or layout any source text. You can apply different Stylesheets to the same source to get different results. XSL John Smith Output: John Smith XSL How can you produce the following output? John Smith XSL

3 Every XSL stylesheet must start with an xsl:stylesheet element. The attribute version='1.0' specifies version of XSL(T) specification. This example shows the simplest possible stylesheet. As it does not contain any information, default processing is used. Hello, world Output: Hello, world

4 An XSL processor parses an XML source and tries to find a matching template rule. If it does, instructions inside the matching template are evaluated. Hello, world. I am fine. Output Hello, world. I am fine.

5 Contents of the original elements can be recovered from the original sources in two basic ways. Stylesheet 1 uses xsl:value-of construct. In this case the contents of the element is used without any further processing. The instruction xsl:apply-templates in Stylesheet 2 is different. The parser further processes selected elements, for which a template is defined. Joe Smith For the next page we use this source

6 Stylesheet 1: Stylesheet 2: Joe Smith Which Stylesheet produces which Output?

7 Parts of an XML document to which a template should be applied are determined by location paths. The required syntax is specified in the XPath specification. Simple cases looks very similar to filesystem addressing. id= Output DDD id=d1

8 Processing always starts with the template match="/". This matches the root node (the node whose only child element is the document element, in our case "source"). Many stylesheets do not contain this element explicitly. When this template is not explicitly given, the implicit template is used (it contains as the sole instruction). This instruction means: process all children of the current node, including text nodes.

9 id= id= Output AAA id=a1 AAA id=a2 Parses only first level under

10 id= Output AAA id=a1 BBB id=b1 BBB id=b2 AAA id=a2 BBB id=b3 BBB id=b4 Recurses into sublevels of

11 A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2. Stylesheet 1: [template: outputs ] Joe Smith Output: [template: firstName outputs Joe ] [template: surname outputs Smith ]

12 A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2. Stylesheet 2: [template: outputs ] Joe Smith Output: [template: source outputs [template: employee outputs [template: firstName outputs Joe ] [template: surname outputs Smith ] ] ]

13 With modes an element can be processed multiple times, each time producing a different result. Stylesheet 2: Output: CCC CCC CCC CCC CCC CCC

14 Axes play a very important role in XSLT – e.g. child axis, for-each. Stylesheet 2: : Document: Output: a2: b4 c1

15 xsl:element generates elements in time of processing. In this example it transforms the sizes to formating tags. Header1 Header3 Bold text Subscript Superscript Output: Header1 Header3 Bold text Subscript Superscript

16 xsl:if instruction enables conditional processing. A typical case of xsl:for-each usage is to add a text between individual entries. Very often you do not want to add text after the last element:, Output: A, B, C, D

17 Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering. - First Chap. Sec. Chap. Sub 1 Sub 2 What could be the possible output? Continued on next page.

18 Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering. Output: 1 - First Chap. 2 - Sec. Chap. 2.1 - Sub 1 2.2 - Sub 2

19 You can set variables in a Stylesheet and use them later in the processing. The following example demonstrates a way of setting xsl:variable. / xsl:for-eachxsl:template Chapter Chapter Chapter Chapter What could be the possible output? Continued on next page.

20 You can set variables in a Stylesheet an use them later in the processing. The following example demonstrates a way of setting xsl:variable. Output: Chapter 1/4 Chapter 2/4 Chapter 3/4 Chapter 4/4


Download ppt "XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended."

Similar presentations


Ads by Google