• Keine Ergebnisse gefunden

ƒ Fragen zur Vorlesung „XSLT“?

N/A
N/A
Protected

Academic year: 2022

Aktie "ƒ Fragen zur Vorlesung „XSLT“? "

Copied!
68
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Ü Ü bung 5 bung 5

(2)

Ü Ü bung 5 bung 5

ƒ Fragen zur Vorlesung „XSLT“?

ƒ XSLT in Depth

ƒ XSLT Programming

ƒ Musterfragen

ƒ Musterlösung

ƒ XSLT for transformation from content to presentation

(3)

XSLT in Depth

XSLT in Depth

(4)

Valid XSLT

<xsl:stylesheet> </xsl:stylesheet>

<xsl:stylesheet

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

Transform"

>

</xsl:stylesheet>

<xsl:stylesheet version="1.0"

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

Transform"

(5)

Valid XSLT

<xsl:stylesheet version="1.0"

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

Transform"

>

</xsl:stylesheet>

<name>

<first_name>John</first_name>

<last_name>Smith</last_name>

</name>

(6)

Valid XSLT

John Smith

How come?

(7)

Never forget about the….

• Default templates

- Are ALWAYS used by the XSLT processor - Can‘t be switched off, only OVERRIDDEN

<xsl:template match="text()|@*">

<xsl:value-of select="."/>

</xsl:template>

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

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="processing-

(8)

Default templates

<name>

<first_name>John</first_name>

<last_name>Smith</last_name>

</name>

name

first_name last_name

„root“(/)

(9)

Overriding templates

• If a match exists in the stylesheet, that template will

„override“ the default template

<xsl:template match="name">

<!–- Do Nothing -->

</xsl:template>

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

<xsl:apply-templates/>

</xsl:template>

overrides

(10)

Overriding templates (2)

<xsl:template match="name">

<!–- Do Nothing -->

</xsl:template>

<xsl:template match="name[2]">

• So what about this? Does the more specific template override the more general?

is overriden by

(11)

Overriding templates (3)

<xsl:template match="name[@lang='en']">

<!–- Do Nothing -->

</xsl:template>

<xsl:template match="name[2]"

<xsl:apply-templates/>

</xsl:template>

• And what about this? What happens if the name element in the second position has also a lang attribute with the value ‚en‘?

???

(12)

Template priority

• „If more than one template rule with the same import precedence matches a given node….“

- Import precedence is default template rules, explicitly imported rules and then rules in the actual stylesheet

• „… the one with the highest priority is chosen“

- Rules with match patterns of a single element or attribute name have priority 0

- Rules with match patterns of prefix:* have priority -0.25 - Rules with match patterns which have only a wildcard test

(*,@*,node() …) have priority -0.5

- Rules with any other patterns, e.g. with location paths or

(13)

Template priority (2)

• „It is an error if two or more template rules match a node and have the same priority“

- Most XSLT processors choose the LAST template rule rather than signal an error

• One can explicitly give a template a priority

<xsl:template match="…" priority="1">

• How to execute multiple rules on the same node?

- Use a more general match on a template rule

- Within the rule, make the specific node selection

- You can use named templates rather than matching on the selected node

(14)

Overriding by import

• You can use

<xsl:import href="import.xsl"/>

• However, imported templates have less precedence than templates in the main stylesheet

• To „force“ the processor to use BOTH an imported template and a template in the main stylesheet:

<xsl:apply-imports />

• The problem is that imports must be top level elements and come before all other elements, hence are always overridden

• The alternative is to include templates at the top level but ANYWHERE in the stylesheet, such as right at the end

(15)

XSLT and namespaces

• XSLT processors are namespace-aware

- Patterns must identify namespaces of elements

<name xmlns="http://www.name.org">

<first_name>John</first_name>

<last_name>Smith</last_name>

</name>

<xsl:template match="name">

<!–- Do Nothing -->

</xsl:template>

(16)

XSLT and namespaces

<name xmlns="http://www.name.org">

<first_name>John</first_name>

<last_name>Smith</last_name>

</name>

<xsl:stylesheet

xmlns="http://www.name.org">

<xsl:template match="name">

<!–- Do Nothing -->

(17)

XSLT and namespaces

<aa:name xmlns:aa="http://www.name.org">

<aa:first_name>John</aa:first_name>

<aa:last_name>Smith</aa:last_name>

</aa:name>

<xsl:stylesheet

xmlns:zz="http://www.name.org">

<xsl:template match="zz:name">

<!–- Do Nothing -->

</xsl:template>

</xsl:stylesheet>

(18)

XSLT Programming

XSLT Programming

(19)

Did you know…

• XSLT is „Turing-complete“

- meaning?

• Turing-completeness:

- Computational system that can compute every Turing- computable function

- Such functions represent the notion of algorithm, i.e. a finite program should be able to compute them

- They are used to measure models of computation

• All general programming languages are Turing-complete - Hence: XSLT is as expressive as such languages!

(20)

Proving XSLT‘s programming power

• XSLT is usually referred to as a „declarative“ programming language

For example, one can state

z = y + 3

The variable z now has the value of y increased by 3

A variable, once declared, can not change value (stateless) In XSLT it looks like this:

<xsl:variable name="z" select="$y + 3" />

(21)

Higher order functions in XSLT

• Functions can be implemented in template references - Templates in XSLT correspond to functions

- Named templates are called by xsl:call-template - Parameters are declared on a template with xsl:param - Parameter values are set when a template is called using

xsl:with-param

• In XSLT one has to call a template specifically by name - You can‘t provide a template name by variable

- Hence you can‘t really „dynamically“ call templates

(22)

Template references

• Define templates which perform some function, e.g.

<xsl:template name="2times">

<xsl:param name = "a" />

<xsl:value-of select = "2 * $a" />

</xsl:template>

<xsl:template name="3plus">

<xsl:param name = "a" />

<xsl:value-of select = "3 + $a" />

(23)

Template references (2)

• Now define a template which accepts as parameter an input value, instantiates the two „function“ templates, passing to each instantiation the input in the template‘s parameter and as result gives the sum of the outputs.

• mySum(x)=(2*x)+(3+x)

• „Call“ mySum(3):

<xsl:call-template name = "mySum" >

<xsl:with-param name = "x" select = "3" />

</xsl:call-template>

(24)

Template references (3)

<xsl:template name = "mySum" >

<xsl:param name = "x" />

<xsl:variable name="v1">

<xsl:call-template name = "2times" >

<xsl:with-param name= "a" select="$x" />

</xsl:call-template>

</xsl:variable>

<xsl:variable name="v2">

<xsl:call-template name = "3plus" >

<xsl:with-param name="a" select="$x" />

</xsl:call-template>

</xsl:variable>

(25)

Template references (4)

How about printing a certain number of dots, where the number is not known at compile time?

In C, it could look like this:

void printDots(int n) {

int i; for (i = 0; i < n; i++) { printf("."); }

}

(26)

Recursive programming

• What most programming languages can accomplish with loops must be solved in functional languages like XSLT with recursion

• Recursion is a powerful programming construct for solving particular classes of problem (divide and conquer)

• Usual construction:

<xsl:template name="…">

Parameters Test

Do something or not (Another Test)

Either

generate new parameter values call template recursively Or

(27)

Recursion example

I presently have an XML tag stored as so:

<rating> 7 </rating>

It is essentially for rating a product out of a possible score of 10.

What I want to do is to convert it like this:

Rating: 1 2 3 4 5 6 7 8 9 10 where the "7" will be made BOLD.

(28)

Recursion example

Define the starting state

<xsl:template name="ratings">

<xsl:param name="limit" select="10"/>

<xsl:param name="this" select="1"/>

<xsl:param name="emph"/>

$this is the parameter that will be tested

$emph is the parameter to be tested against (when the template

(29)

Recursion example

Define the test

<xsl:choose>

<xsl:when test="$this=$emph">

<b>&#xa0; <xsl:value-of select="$this"/></b>

</xsl:when>

<xsl:otherwise>

&#xa0; <xsl:value-of select="$this"/>

</xsl:otherwise>

</xsl:choose>

(30)

Recursion example

Define the recursion. Don‘t forget an exit condition!

<xsl:if test="$this &lt; $limit">

<xsl:call-template name="ratings">

<xsl:with-param name="limit" select="$limit"/>

<xsl:with-param name="this" select="$this+1"/>

<xsl:with-param name="emph" select="$emph"/>

</xsl:call-template>

</xsl:if>

(31)

Musterfragen

Musterfragen

(32)

Frage 1 Frage 1

Which of the following XSLT elements will apply any matching templates to all the children of the current node?

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

B. <xsl:call-template name="*" />

C. <xsl:apply-templates />

D. <xsl:call-template />

E. <xsl:apply-templates name="*" />

(33)

Frage 2 Frage 2

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

<periodicTable>

<chemicalElement symbol="Ag">

<atomicNumberatomicNumber>47</atomicNumberatomicNumber>

<atomicWeight>107.8682</atomicWeight atomicWeight>atomicWeight

</chemicalElement>

</periodicTable>

Which is the output from

<xsl:copy/> ?

A. <chemicalElement/>

B. <chemicalElement symbol="Ag"/>

C. <chemicalElement symbol="Ag"> …

</chemicalElement>

D. 47 107.8682

(34)

Frage 3 Frage 3

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

<periodicTable>

<chemicalElement symbol="Ag">

<atomicNumberatomicNumber>47</atomicNumberatomicNumber>

<atomicWeight>107.8682</atomicWeight atomicWeight>atomicWeight

</chemicalElement>

</periodicTable>

Which is the output from

<xsl:copy-of select="." /> ? A. <chemicalElement/>

B. <chemicalElement symbol="Ag"/>

C. <chemicalElement symbol="Ag"> …

(35)

Frage 4 Frage 4

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

<periodicTable>

<chemicalElement symbol="Ag">

<atomicNumberatomicNumber>47</atomicNumberatomicNumber>

<atomicWeight>107.8682</atomicWeight atomicWeight>atomicWeight

</chemicalElement>

</periodicTable>

Which is the output from

<xsl:value-of select="." /> ? A. <chemicalElement/>

B. <chemicalElement symbol="Ag"/>

C. <chemicalElement symbol="Ag"> …

</chemicalElement>

D. 47 107.8682

(36)

Frage 5 Frage 5

<?xml version="1.0"

encoding="UTF-8"?>

<A>

<B c="123">

<C />

<D>text</D>

</B>

<D>

<E>more text</E>

<F c="246" />

</D>

<F a="369" />

Which is the output?

A. Nothing

B. 123 text more text 246 369 C. 123 246 369

<xsl:template match="A">

<xsl:apply-templates />

</xsl:template>

<xsl:template match="B|D|F">

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

</xsl:template>

(37)

Frage 6 Frage 6

<?xml version="1.0"

encoding="UTF-8"?>

<A>

<B c="123">

<C />

<D>text</D>

</B>

<D>

<E>more text</E>

<F c="246" />

</D>

<F a="369" />

</A>

Which is the output?

A. Nothing B. text

C. text more text

D. 123 text more text 246

<xsl:template match="B|D|F">

<xsl:if test="node()/text()">

<xsl:value-of select="." />

</xsl:if>

</xsl:template>

(38)

Frage 7 Frage 7

<?xml version="1.0"

encoding="UTF-8"?>

<A>

<B c="123">

<C />

<D>text</D>

</B>

<D>

<E>more text</E>

<F c="246" />

</D>

<F a="369" />

Which is the output?

A. Nothing

B. text more text 246 369 C. text 246 369

<xsl:template match="D">

<xsl:apply-templates select="F" />

</xsl:template>

<xsl:template match="F">

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

</xsl:template>

(39)

Musterl

Musterl ö ö sung des sung des

Ü Ü bungsblattes 4 bungsblattes 4

(40)

Beispieltransformation

<document xmlns="http://www.doc.org/ns" …>

<title>Beginning XML</title>

<author>Hunter et al.</author>

<chapter name="Chapter 1">

<section>Chap. 1_1</section>

<section>Chap. 1_2</section>

<section>Chap. 1_3</section>

</chapter>

<chapter name="Chapter 2">

<section>Chap. 2_1</section>

<section>Chap. 2_2</section>

</chapter>

<chapter name="Chapter 3">

<section>Chap. 3_1</section>

<section>Chap. 3_2</section>

(41)

Ergebnisdokument

(42)

Nötige strukturelle Transformation

(43)

Klasse der Ursprungsdokumente

(44)

Grundstruktur der Lösung

ƒ für jedes Element außer ns:section ein Template

(45)

Erzeugung der HTML-Grundstruktur

(46)

Transformation von title und author

(47)

ns:title Î <h1>, ns:author Î <h2>

ƒ Was passiert, wenn title oder author Kind-Elemente

haben?

(48)

Select . or text() ? Select . or text() ?

<xsl:template match="p">

<DIV>

<xsl:copy<xsl:copy--ofof select="."/>select="."/>

</DIV>

<DIV>

<xsl:copy<xsl:copy/>/>

</DIV>

<DIV>

<xsl:value<xsl:value--ofof select="."/>select="."/>

</DIV>

</xsl:template>

<source>

<p id="a12">Compare

<p id="a12">Compare

<DIV>

<p id="a12">Compare

<p id="a12">Compare

<B>these constructs</B>.

<B>these constructs</B>.

</p>

</p>

</DIV>

<DIV>

<p/>

<p/>

</DIV>

<DIV>

Compare these constructs.

Compare these constructs.

</DIV>

oder mit "text()" statt "."

(49)

Spalten

Spalten ü ü berschriften erzeugen berschriften erzeugen

(50)

Grundstruktur der Tabelle erzeugen

Grundstruktur der Tabelle erzeugen

(51)

Spalten

Spalten ü ü berschriften <TH> erzeugen berschriften <TH> erzeugen

(52)

Zeilen <TR> erzeugen

Für N von 1 bis zur maximalen Anzahl von sections:

(53)

Erzeuge Zeile N!

(54)

Erzeuge Zeile N!

(55)

Vordefinierte Templates Vordefinierte Templates

1. 1. vordefiniertes Template vordefiniertes Template

ƒ realisiert rekursiven Aufruf des Prozessors, wenn kein Template anwendbar ist:

2. 2. vordefiniertes Template vordefiniertes Template

ƒ kopiert PCDATA und Attribut-Werte des aktuellen Knotens in das Ergebnisdokument:

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

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="text()|@*">

<xsl:value-of select="."/>

(56)

Vermeide <TD/> !

(57)

Rekursion: Erzeuge Zeile N+1

(58)

Und der eigentliche Aufruf mit N=1

(59)

XSLT for content to XSLT for content to

presentation

presentation

(60)

Content to presentation

• Besides content-to-content transformations, XSLT is often

used to transform a content format (i.e. some XML document) into a presentation format

• The output mustn‘t be well formed XML, it could be HTML or some other unstructured textual format

• However, there are XML-based presentation formats (XHTML, SVG, SMIL)

• XML-to-XML transformation is preferable

- As XSLT templates work on a XML input structure, it is an easier mapping to a XML output structure

- It is also easier to check output validity

(61)

RSS to SMIL

<smil>

<head>

<layout>

<root-layout width="450“ height="450"/>

<region id="top" top="0" left="0"

width="450" height="100"/>

<region id="text" top="100" left="0"

width="250" height="100"/>

<region id="desc" top="200" left="0"

width="250" height="250" />

<region id="img" top="100" left="250"

width="200" height="350" />

</layout>

</head>

<body>

...

</body>

</smil>

(62)

RSS to SMIL (2)

<rss>

<channel>

<title>….</title>

<item>

<title>Star City</title>

<enclosure>…</enclosure>

<description>….</description>

</item>

<item>

<title>Star City</title>

<enclosure>…</enclosure>

<description>….</description>

<smil>

<head>

<layout>

...

</layout>

</head>

<body>

<text src=".." region="top" />

<seq begin="0s">

<par dur="10s">

<text src=".." region="text" />

<text src=".." region="desc" />

<image src=".." region="img" />

</par>

<par dur="10s">

(63)

RSS to SMIL (3)

<rss>

<channel>

<title>….</title>

<item>

<title>Star City</title>

<enclosure>…</enclosure>

<description>….</description>

</item>

<item>

<title>Star City</title>

<enclosure>…</enclosure>

<description>….</description>

</item>

</channel>

<smil>

<head>

<layout>

...

</layout>

</head>

<body>

<text src=".." region="top" />

<seq begin="0s">

<par dur="10s">

<text src=".." region="text" />

<text src=".." region="desc" />

<image src=".." region="img" />

</par>

<par dur="10s">

</par>

</seq>

</body>

(64)

The XSLT

<xsl:template match="/">

<xsl:apply-templates select="rss" />

</xsl:template>

Why not just start writing e.g. template match="//item"?

Because we want to ensure we are transforming a RSS document, and not just some XML document with item elements!

(65)

The XSLT (2)

<xsl:template match="rss">

<smil>

<head>

<layout>

...

</layout>

</head>

<body>

<xsl:apply-templates select="title" />

<seq>

<xsl:apply-templates select="item" />

</seq>

</body>

</smil>

</xsl:template>

(66)

The XSLT (3)

<xsl:template match="title">

<text src="data:,{.}" region="top" />

</xsl:template>

Reminder:

<xsl:value-of select="." /> returns the string value of the element node (the text within the element tags)

We can‘t put the value-of element inside an output element, so we use an attribute value template

This means placing the XPath expression inside curly braces {}

(67)

The XSLT (4)

<xsl:template match="item">

<par dur="10s">

<text src="data:,{title}" region="text" />

<text src="data:,{description}" region="desc" />

<image src="{enclosure/@url}" region="img" />

</par>

</xsl:template>

Reminder:

Template will be called for each item element in document order Why not use separate templates for the child elements of item?

We assumed enclosures were images, how could we write

<image>, <audio> or <video> elements based on the value of enclosure‘s type attribute?

(68)

Viewing in the browser

• To have a RSS (or any XML) document shown in the browser with style rules applied

<?xml-stylesheet href="rss.xsl" type="text/xsl"

media="screen"?>

• Generally used with stylesheets with (X)HTML output

• If the browser is correctly configured, a SMIL output should be opened in a SMIL player

Referenzen

ÄHNLICHE DOKUMENTE

Beispieldatei aus

A face object is represented by the appearance of the eyes, nose and mouth, and a shape model that code how these parts can deform.. A body object is represented by the appearance of

n Klassen-Template: anstatt eines konkreten Typs für ein Attribut einer Klasse oder einen Parameter einer Methode wird ein.. n Parametertyp T,

• aktueller Teilbaum: Baum, der vom aktuellen Knoten aufgespannt wird, einschließlich aller Attribute und PCDATA. •

• aktueller Teilbaum: Baum, der vom aktuellen Knoten aufgespannt wird, einschließlich aller Attribute und PCDATA. •

• alle Einträge bestimmen, die einen Nachnamen besitzen, der nicht bereits bei einem vorhergehenden Eintrag zu finden ist. Bestimmen aller Einträge (contact) auf den gleichen

Template Method lets subclasses redefine certain steps of an algortihm without changing the algorithm‘s structure“.. - Design Patterns, Gamma

Nachteil: hohe Compile-Time weil template code mehrfach übersetzt wird Folge: man muss beim Erzeugen von object-files die template DEFINITION zur Verfügung stellen -&gt;