Upload
others
View
31
Download
0
Embed Size (px)
Citation preview
Übersicht
• Namespaces
• XPath
• einfache XSLT-Stylesheets
– template rules
– default rules
XML-Praxis XSLT – XSL Transformations, Teil 1 2/22
Namespaces
• XML-Sprachen für wiederkehrende Probleme:
– Tabellen
– mathematischer Formelsatz
– genetische Sequenzen
– . . .
• Kombination/Einbettung von Sprachen
• Beispiel: (X)HTML-Dokument mit Formeln in MathML
• Problem: Was gehört zu welcher XML-Sprache?
XML-Praxis XSLT – XSL Transformations, Teil 1 3/22
Verwendung von Namespaces
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>...</head>
<body>
<p>also sprach Pythagoras:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" >
<mrow>
<msup><mi>x</mi><mn>2</mn></msup>
<mo>+</mo>
<msup><mi>y</mi><mn>2</mn></msup>
...
</math>
</body>
</html>
XML-Praxis XSLT – XSL Transformations, Teil 1 4/22
alternative Notation von Namespaces
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ml="http://www.w3.org/1998/Math/MathML" >
<head>...</head>
<body>
<p>also sprach Pythagoras:</p>
<ml:math>
<ml:mrow>
<ml:msup><ml:mi>x</ml:mi><ml:mn>2</ml:mn></ml:msup>
<ml:mo>+</ml:mo>
<ml:msup><ml:mi>y</ml:mi><ml:mn>2</ml:mn></ml:msup>
...
</ml:math>
</body>
</html>
XML-Praxis XSLT – XSL Transformations, Teil 1 5/22
Namespaces, cont.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ml="http://www.w3.org/1998/Math/MathML">
• default namespace
• namespace prefix beliebig
• namespace URI ausschlaggebend, muß exakt übereinstimmen
• keine Verknüpfung mit Grammatik/Schema
• URL als identifier extrem schlechte Wahl
• schlecht mit DTDs zu realisieren
XML-Praxis XSLT – XSL Transformations, Teil 1 6/22
XML-Dokument als Baum
presentation
texttitle
date
status
author
root
title toc
slide
slide
textitem
text
text
item
itemilist
text
text
slide
• weitere Text-Knoten durch whitespace
• Aufgabe: lokalisiere einen (oder mehrere) Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 7/22
Analogie Dateisystem
homes/ juser
joern
vol gnu bin
lib
share
local
tex
ls
manual.txt
• absoluter Pfad: Wegbeschreibung vom Wurzelverzeichnis
/vol/gnu/bin/ls
• relativer Pfad: Wegbeschreibung vom „aktuellen“ Verzeichnis
../juser/manual.txt
• Unterschied bei XML: gleichnamige Kindknoten
XML-Praxis XSLT – XSL Transformations, Teil 1 8/22
XPath
• XPath beschreibt Pfade im XML-Baum
• Knotentypen: Elementknoten, Attributknoten, Textknoten, . . .
• Achsen: child, parent, sibling, descendant, . . . , attribute
• Beispiele:
slide
slide/title
/presentation/author/text()
/presentation/@status
//slide/title
• ausführliche Schreibweise:
/child::presentation/attribute::status
XML-Praxis XSLT – XSL Transformations, Teil 1 9/22
XPath, cont.
• Bedingungen in XPath-Ausdrücken:
/presentation/slide/title[@toc="yes"]
/presentation/slide/[title/@toc="yes"]/ilist
/presentation/slide[position()=2]
/presentation/slide[position()=last()]
• Funktionen:
count(/presentation/slide)
/presentation/slide[contains(title,"XML")]
• arithmetische Funktionen
• String-Funktionen
XML-Praxis XSLT – XSL Transformations, Teil 1 10/22
Eigenschaften von XSL
• Ursprünge:
– CSS (Cascading Style Sheets)
– DSSSL (Document Style and Semantics Specification Language)
• auf XML übertragen: XSL (Extensible Stylesheet Language)
• zwei Teile:
– XSLT (XSL Transformations)
– XSL-FO (XSL Formatting Objects)
XML-Praxis XSLT – XSL Transformations, Teil 1 11/22
Eigenschaften von XSLT
• Transformation zwischen XML-Bäumen
• vollständige Programmiersprache
• Bezeichnung „style sheet“ hat sich trotzdem gehalten
• deklarativ, ohne Seiteneffekte
• ähnlich zu funktionalen Sprachen (Haskell)
• regelbasiert
• ist selber XML
XML-Praxis XSLT – XSL Transformations, Teil 1 12/22
ein einfaches Stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1"/>
<xsl:template match="/presentation">
<html>
<head>
<title>Presentation</title>
</head>
<body>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML-Praxis XSLT – XSL Transformations, Teil 1 13/22
ein einfaches Stylesheet, cont.
• HTML hinreichend ähnlich zu XML
• Ausgabe:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title>Presentation</title>
</head>
<body>
</body>
</html>
XML-Praxis XSLT – XSL Transformations, Teil 1 14/22
templates
• XSLT-Prozessor verarbeitet Dokument entsprechend Baumstruktur
• zum aktuellen Knoten passendes template wird ausgeführt
<xsl:template match="/presentation">
<html>
...
</html>
</xsl:template>
• Text innerhalb des templates wird ausgegeben
• Stylesheet muß wohlgeformt sein
• XSLT-Anweisungen werden verarbeitet
XML-Praxis XSLT – XSL Transformations, Teil 1 15/22
templates kombinieren
• Kontrolle an XSLT-Prozessor zurückgeben
<xsl:template match="/presentation">
<html>
<head>...<head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
• rekursive Verarbeitung des nächsten Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 16/22
templates kombinieren, cont.
<ilist>
<item>XML is ...</item>
<item>XML instances ...</item>
</ilist>
XML-Praxis XSLT – XSL Transformations, Teil 1 17/22
templates kombinieren, cont.
<ilist>
<item>XML is ...</item>
<item>XML instances ...</item>
</ilist>
<ul>
<li>XML is ...</li>
<li>XML instances ...</li>
</ul>
XML-Praxis XSLT – XSL Transformations, Teil 1 17/22
templates kombinieren, cont.
<ilist>
<item>XML is ...</item>
<item>XML instances ...</item>
</ilist>
<ul>
<li>XML is ...</li>
<li>XML instances ...</li>
</ul>
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
XML-Praxis XSLT – XSL Transformations, Teil 1 17/22
default rules
• template rule für item :
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rules:
element/root node verarbeite Kind-Knoten
text node gib Text aus
attribute/comment node ignorieren
• Stylesheets können top-down erstellt werden
XML-Praxis XSLT – XSL Transformations, Teil 1 18/22
der Transformationsprozeß
item
item text
textitemilist
text
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li text
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li text
li
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li text
li text
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li text
li text
li
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
der Transformationsprozeß
item
item text
textitemilist
text
ul li text
li text
li text
<xsl:template match="ilist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
• default rule für Text-Knoten
XML-Praxis XSLT – XSL Transformations, Teil 1 19/22
templates, cont.
• match-Attribut vollständiger XPath-Ausdruck:
<xsl:template match="presentation/title">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:template match="slide/title">
<h2>
<xsl:number count="slide"/>: <xsl:apply-templates/>
</h2>
</xsl:template>
XML-Praxis XSLT – XSL Transformations, Teil 1 20/22
Daten auswählen
• Verarbeitung in Dokumentreihenfolge nicht immer sinnvoll
• HTML-Seite mit Titel:
<xsl:template match="/presentation">
<html>
<head>
<title>
<xsl:value-of select="title"/>
</title>
</head>
...
</xsl:template>
• XPath-Pfad vom aktuellen Knoten aus
XML-Praxis XSLT – XSL Transformations, Teil 1 21/22
Daten auswählen, cont.
• Navigationselemente: vorherige Folie
<xsl:value-of select="preceding-sibling::slide[1]/title"/>
• preceding /preceding-sibling liefern Knoten in umgekehrterDokumentreihenfolge
• analog nächste Folie
<xsl:value-of select="following-sibling::slide[1]/title"/>
XML-Praxis XSLT – XSL Transformations, Teil 1 22/22