xslt, a functional approach to xml document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfthe...

54
XSLT, a functional approach to XML document transformation Ludovic Stordeur June 7, 2011

Upload: others

Post on 12-Mar-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XSLT, a functional approach to XML documenttransformation

Ludovic Stordeur

June 7, 2011

Page 2: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 3: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 4: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Context

1. Performing my first Open-MX release;

2. Tarball generation, GForge pushing, . . .

3. and finally . . . website update !

I a HTML handwritten code;I Need to add the Open-MX release news at several places;

I On the Releases page;I On the News page;I On the main page in the “hot” news section.

A lot of code duplication !Need for a more maintainable solution.

Page 5: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Context

1. Performing my first Open-MX release;

2. Tarball generation, GForge pushing, . . .

3. and finally . . . website update !I a HTML handwritten code;I Need to add the Open-MX release news at several places;

I On the Releases page;I On the News page;I On the main page in the “hot” news section.

A lot of code duplication !Need for a more maintainable solution.

Page 6: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 7: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Goals

IdeaSplit “cold” part and “hot” part of the website.

1. Inputs

I “cold” part: several XHTML documents;I “hot” part: our own XML dialect;

I Releases;I News.

2. Processing phase

3. Output

The final website: a collection of XHTMLdocuments.

Solution?

releases & news(XML dialect)

"cold" part(XHTML documents)

Final website(XHTML documents)

Page 8: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Existing approaches

Based on a “classical” XML parsing . . .

I SAX implementations;

I Expat;

I libxml2;

I . . .

. . . using general purpose programming languages

I PHP;

I Python;

I Java;

I C/C++;

I . . .

Error-prone solutions !

Page 9: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Existing approaches

Based on a “classical” XML parsing . . .

I SAX implementations;

I Expat;

I libxml2;

I . . .

. . . using general purpose programming languages

I PHP;

I Python;

I Java;

I C/C++;

I . . .

Error-prone solutions !

Page 10: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XSLT: a different point of view

XSLT: Extensible Stylesheet Language Transformations

I XML-based language;

I Higher level of abstraction (declarative model);

I Functional paradigm;

I XML dedicated processing.

Page 11: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 12: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

History

I Developed by the World Wide Web Consortium (W3C);I Originally part of the Extensible Stylesheet Language (XSL)

project;I XSL Formatting Objects;I XPath.

I Development effort initiated in 1998;

I XSLT 1.0: first recommandation on 16 November 1999(James Clark);

I XSLT 2.0: 23 January 2007 (Michael Kay);

I Direct successor of DSSSL (SGML processing).

Page 13: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 14: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Overview

Involved entities

I XML inputs (generally one document);

I XSLT code inputs (generally one module);

I XSLT processor;I Outputs (generally one document);

I XML (XHTML);I HTML;I Plain text;I Binary.

Philosophy

I Declaration of a collection of rules;I A rule define how to handle a given input

node;I Using pattern matching through XPath.

XML Input XSLT Code

XSLT Processor

<xsl:value-of>Title:$nameDate:$curdat</xsl:value-o

Result Document

source: Wikipedia

Page 15: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 16: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XML description of the releases

<?xml version="1.0" encoding="utf-8"?>

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" date="2010/12/16"

md5="4565e6516610a2776e4a926529885f82"

href="28070/open-mx-1.3.4.tar.gz"/>

<release ver="1.3.3" date="2010/10/15"

md5="253b46bc0151d1c6f3ea347006dab85b"

href="27637/open-mx-1.3.3.tar.gz"/>

...

</releases>

</open-mx>

Page 17: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Wished output

...

<tr>

<td> <a

href="http://gforge.inria.fr/frs/download.php/28070/open-mx-1.3.4.tar.gz">

Open-MX 1.3.4

</a> </td>

<td>2010/12/16</td>

<td>4565e6516610a2776e4a926529885f82</td>

</tr>

<tr>

<td> <a

href="http://gforge.inria.fr/frs/download.php/27637/open-mx-1.3.3.tar.gz">

Open-MX 1.3.3

</a> </td>

<td>2010/12/16</td>

<td>253b46bc0151d1c6f3ea347006dab85b</td>

</tr>

...

Page 18: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

The minimal XSLT skeleton

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

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

<xsl:output method="xml"

version="1.0"

encoding="utf-8"

indent="yes"

doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

<!-- XSLT code here -->

</xsl:stylesheet>

Page 19: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Introduction to templates

<xsl:stylesheet>

<xsl:output ...>

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

<a>

<xsl:attribute name="href">

<xsl:value-of select="../base url"/>/<xsl:value-of select="@href"/>

</xsl:attribute>

Open-MX <xsl:value-of select="@ver"/>

</a>

</td>

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

<td> <xsl:value-of select="@md5"/> </td>

</tr>

</xsl:template>

</xsl:stylesheet>

Page 20: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. The XSLT processor permanently have a context in the XMLinput;

2. Context = Node.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 21: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Starting context: the root node;

2. Looking for a matching rule . . .

3. Context change: go down to the child node.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 22: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Starting context: the root node;

2. Looking for a matching rule . . .

3. Context change: go down to the child node.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 23: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Starting context: the root node;

2. Looking for a matching rule . . . Failed !

3. Context change: go down to the child node.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 24: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Looking for a matching rule . . .

2. Context change: go down to the child node . . .

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 25: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Looking for a matching rule . . . Failed !

2. Context change: go down to the child node . . .

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 26: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Which ones ?

2. all of them but in a completely specified order;

3. New definition: Context = Nodeset.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 27: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Which ones ?

2. all of them but in a completely specified order;

3. New definition: Context = Nodeset.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 28: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Which ones ?

2. all of them but in a completely specified order;

3. New definition: Context = Nodeset.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 29: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Matching!

2. Executing the template code;

3. The next child will also match the same template.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 30: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Matching!

2. Executing the template code;

3. The next child will also match the same template.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

--> XPath expression

<tr>

<td>

...

Page 31: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Core concept

1. Matching!

2. Executing the template code;

3. The next child will also match the same template.

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release"> --> XPath expression

<tr>

<td>

...

Page 32: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XPath

Goal

1. Addressing a part of the XML tree;

2. Short syntax;

3. Powerful language.

Recommendation

XPath 1.0 16 November 1999;

XPath 2.0 23 January 2007.

Page 33: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XML tree

Page 34: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

The XPath concept: XPath path

DefinitionA step sequence:

[/]step1/step2/.../stepN

I A step is evaluated in a nodeset context;

I A step evaluation provides the context for the next step;

I The last step evaluation returns the final nodeset.

Initial context

Absolute path the root node;

Relative path the current node (current context in the XMLdocument).

Path unionUsing the operator: |

/foo | /bar/baz

Page 35: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XPath steps

Definitionaxe::filter[predicate]*

axe scanning direction;

filter first-level filtering;

predicates second-level filtering.

Some filters

QName filter nodes having the name QName;

* nodes of the main type;

node() any node;

text() text nodes.

Page 36: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

XPath syntactic sugars

child:: can be omitted

child::releases/child::release

--> releases/release

attribute:: can be replaced by @

child::release[attribute::ver="1.3.3"]/attribute::md5

--> release[@ver="1.3.4"]/@md5

descendant-or-self::node() can be replaced by //

descendant-or-self::node()/@md5

--> //@md5

Page 37: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Template application

I Non XSLT elements: inlined in the output document.

<xsl:stylesheet>

...

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

<a>

<xsl:attribute name="href">

<xsl:value-of select="../base url"/>/<xsl:value-of select="@href"/>

</xsl:attribute>

Open-MX <xsl:value-of select="@ver"/>

</a>

</td>

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

<td> <xsl:value-of select="@md5"/> </td>

</tr>

</xsl:template>

...

</xsl:stylesheet>

Page 38: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Template application

I Non XSLT elements: inlined in the output document.

<xsl:stylesheet>

...

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

<a>

<xsl:attribute name="href">

<xsl:value-of select="../base url"/>/<xsl:value-of select="@href"/>

</xsl:attribute>

Open-MX <xsl:value-of select="@ver"/>

</a>

</td>

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

<td> <xsl:value-of select="@md5"/> </td>

</tr>

</xsl:template>

...

</xsl:stylesheet>

Page 39: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Template applicationxsl:attribute

I XSLT function;

I Create a computed attribute in the output document.

<xsl:stylesheet>

...

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

<a>

<xsl:attribute name="href">

<xsl:value-of select="../base url"/>/<xsl:value-of select="@href"/>

</xsl:attribute>

Open-MX <xsl:value-of select="@ver"/>

</a>

</td>

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

<td> <xsl:value-of select="@md5"/> </td>

</tr>

</xsl:template>

...

</xsl:stylesheet>

Page 40: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Template application

xsl:value-of

I Extract a node value.

<xsl:stylesheet>

...

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

<a>

<xsl:attribute name="href">

<xsl:value-of select="../base url"/>/<xsl:value-of select="@href"/>

</xsl:attribute>

Open-MX <xsl:value-of select="@ver"/>

</a>

</td>

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

<td> <xsl:value-of select="@md5"/> </td>

</tr>

</xsl:template>

...

</xsl:stylesheet>

Page 41: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Some XSLT functions

xsl:element Create a computed element node;

xsl:text Create a computed text node;

xsl:comment Create a computed comment node;

xsl:if Conditional execution;

xsl:choose aka “switch. . . case”;

xsl:for-each Iteration over a nodeset.

. . . and a lot more !

Page 42: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Force a new context (xsl:apply-templates)

Goal

I We just want to generate the release “1.3.4”;

I Idea: manually build the next context.

XML input:

<open-mx>

<releases> <-- Current context

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases">

<xsl:apply-templates select="release[@ver=’1.3.4’]">

</xsl:template>

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

Page 43: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Force a new context (xsl:apply-templates)Solution: apply-templates

I Create the next context, using an XPath expression;

I Push it as the new context.

XML input:

<open-mx>

<releases> <-- Current context

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases">

<xsl:apply-templates select="release[@ver=’1.3.4’]">

</xsl:template>

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

Page 44: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Force a new context (xsl:apply-templates)Solution: apply-templates

I Create the next context, using an XPath expression;

I Push it as the new context.

XML input:

<open-mx>

<releases> <-- Stacked context

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ... <-- New current context

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases">

<xsl:apply-templates select="release[@ver=’1.3.4’]">

</xsl:template>

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

Page 45: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Default templates

I Default “context pushing” is in fact performed using animplicit template;

I Template priority: the first one matches !

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

<xsl:template match="*|/">

</xsl:template>

Page 46: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Default templatesI Default “context pushing” is in fact performed using an

implicit template;I Template priority: the first one matches !

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

<xsl:template match="*|/">

<xsl:apply-templates/>

</xsl:template>

Page 47: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Default templatesI Default “context pushing” is in fact performed using an

implicit template;I Template priority: the first one matches !

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

<xsl:template match="*|/">

<xsl:apply-templates/>

</xsl:template>

Page 48: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Default templatesI Default “context pushing” is in fact performed using an

implicit template;I Template priority: the first one matches !

XML input:

<open-mx>

<releases>

<base url>http://gforge.inria.fr/frs/download.php</base url>

<release ver="1.3.4" ...

<release ver="1.3.3" ...

...

XSLT code:

<xsl:template match="/open-mx/releases/release">

<tr>

<td>

...

<xsl:template match="*|/">

<xsl:apply-templates select=node()/>

</xsl:template>

Page 49: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 50: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Using a standalone program (aka “Server side”)

Concept: two-step cycle

1. Editing XML and/or XSLT documents;

2. Generation by manually calling the XSLT processor.

Syntax example

$ xsltproc -o baz.xhtml foo.xsl bar.xml

Some XSLT processors

Xalan Apache fundation. API C, Java, Perl. XSLT 1.0;

libxslt GNOME project. API C. XSLT 1.0;

Saxon Michael Kay. API Java, .NET. XSLT 1.0 & 2.0;

. . .

Page 51: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Using a web browser (aka “Client side”)

Concept: “on-the-fly” processing

I No longer processing phase on the server side;

I The web browser fetches the XSLT code when requesting anXML resource and performs the process on its side.

Additional annotation in the XML documentIndicate to the browser where is the XSLT stylesheet bound to thedocument:<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>

Limitations

I Supported by most popular web browsers . . . but oftenpartially;

I Less flexibility than the “Server side” approach.

Page 52: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Outline

Context

Goals

History

Overview

Let’s discover XSLT

Concrete use

Conclusion

Page 53: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Conclusion

The essential

I A XML-dedicated manipulation language;

I A very powerful language (nearly everything is possible !);

I Perfectly suited for web domain . . . but not only . . .

Some drawbacks

I Support lacking especially in web browsers;

I XSLT 2.0 not yet widely supported (and so used);

I Language verbosity.

Page 54: XSLT, a functional approach to XML document …sed.bordeaux.inria.fr/seminars/xslt_20110607.pdfThe XPath concept: XPath path De nition A step sequence: [/]step1/step2/.../stepN I A

Thanks !Any questions ?