Adlib gebruikersgroep - najaarsbijeenkomst 2018 - Lotte Baltussen en Caecilia...
Het gebruik van xslt in adlib
1. Het gebruik van XSLT in Adlib
Adlib gebruikersgroep
30-3-2010
Bert Degenhart Drenth
Adlib Information Systems
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
2. Wat is XSLT?
• eXtensible Stylesheet Language
Transformation
• Neemt XML als invoer en transformeert dit
naar iets anders
• Uitvoer kan XML, HTML of Tekst zijn.
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
3. Relevantie voor Adlib
• XSLT wordt in Internet Server gebruikt
• XSLT kan als uitvoerformaat gebruikt worden
• XSLT kan gebruikt worden bij
gegevensconversie
• In 6.6 browserveld, opmaak in één veld
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
4. Basis XML
• Elk XML document begint met een XML
declaratie
<?xml version="1.0" encoding="utf-8"?>
• XML is altijd Unicode
• Een XML document heeft 1 root node
• XML tags zijn vrij te kiezen, soms is er een DTD
(Document Type Definition) of XML Schema
(.xsd) die aangeeft welke tags mogelijk zijn.
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
5. Basis XML
• Tags zijn case sensitive
• Tags komen altijd in paren voor
<open></open>
• Mag verkort worden als <open/>
• Attributen: <open attr=“value”/>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
6. Name spaces
• Een XML tag heeft een “voornaam”en een
“achternaam” (de achternaam komt eerst,
gevolgd door een ‘:’, met daarna de
achternaam) <adlib:title>
• De achternaam heet ook wel name space
• Een name space wordt geïdentificeerd door
een URI (=Uniform Resource Identifier)
• Vaak is een URI ook een URL
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
7. Name spaces 2
• Een name space krijgt betekenis door zijn URI:
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
• De URI van een name space is zoiets als een BSN: het
geeft betekenis aan een naam
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
9. Adlib XML
• Root = <adlibXML>
• Eén node <recordList>
• Per record een node <record>
• Eén <diagnostic> node
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
11. Adlib unstructured XML
• Per veld één XML element
• Veldnaam in het Engels
• Indien geen veldnaam voorhanden is wordt de
tag als veldnaam gebruikt
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
13. XSLT als uitvoerformat
• Input is unstructured XML
• Uitvoerformaat toevoegen als een normale
outputjob.
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
14. XSLT, here we go !
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Verklaar de 2e regel ?
Waarom is http://www.w3.org/1999/XSL/Transform verplicht ?
XSLT is zelf geschreven als XML!
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
15. XSLT is een “pattern matching”taal
Kies een output method
<xsl:output method="xml" indent="yes"/>
•html
•xml
•text
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
16. <xsl:template>
<xsl:template match=“/“>
Dit is de root
</xsl:template>
“/” is een xPath expressie
Beter:
<xsl:template match="/">
<xsl:text>Dit is de root</xsl:text>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
17. <xsl:apply-templates>
<xsl:template match="/">
<xsl:text>Dit is de root</xsl:text>
<xsl:apply-templates select="adlibXML"/>
</xsl:template>
<xsl:template match="adlibXML">
<xsl:text>adlibXML is gevonden!</xsl:text>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
18. <xsl:apply-templates>
<xsl:template match="/">
<xsl:text>Dit is de root</xsl:text>
<xsl:apply-templates select="adlibXML"/>
</xsl:template>
<xsl:template match="adlibXML">
<xsl:apply-templates select="recordList"/>
</xsl:template>
<xsl:template match="recordList">
<xsl:text>recordList is gevonden!</xsl:text>
<xsl:apply-templates select="record"/>
</xsl:template>
<xsl:template match="record">
<xsl:text>Record!</xsl:text>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
19. Internet Explorer is je vriend
• Exporteer met adlwin je test set data
• Open je output file in IE
• Maak een xslt style sheet
(beide files moeten goed getoond worden)
• Voeg aan je xml file toe:
<?xml-stylesheet href="applytemplates.xslt"
type="text/xsl"?>
• Toon je XML file in je browser...
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
20. Of.. Download X-factor
http://eu.dummies.com/store/product/XSLT-For-
Dummies.productCd-0764536516,navId-322468,descCd-
DOWNLOAD.html
De url’s in het boek werken niet!
Zoek naar XSLT for dummies en
daarna in de download sectie!
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
21. X-factor
Niet op Windows 7
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
22. We maken er HTML van
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="adlibXML"/>
</xsl:template>
<xsl:template match="adlibXML">
<xsl:apply-templates select="recordList"/>
</xsl:template>
<xsl:template match="recordList">
<table>
<xsl:apply-templates select="record"/>
</table>
</xsl:template>
<xsl:template match="record">
<tr>
<td>Record</td>
</tr>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
23. En een beetje stijl
<xsl:template match="/">
<head>
<link href="C:UsersPublicDocumentsmijnstijl.css" type="text/css“
rel="stylesheet"/>
</head>
<xsl:apply-templates select="adlibXML"/>
</xsl:template>
<xsl:template match="adlibXML">
<body>
<xsl:apply-templates select="recordList"/>
</body>
</xsl:template>
<xsl:template match="record">
<tr class="record">
<td>Record</td>
</tr>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
24. <xsl:value-of>
• Drukt de uitkomst van een xpath expressie uit
• Simpelste expressie : ‘.’ = inhoud huidige node
<xsl:template match="record">
<tr class="record">
<td>
<xsl:apply-templates select="priref"/>
</td>
</tr>
</xsl:template>
<xsl:template match="priref">
<xsl:value-of select="."/>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
25. <xsl:value-of>
<td>
<xsl:apply-templates select="priref"/>
<xsl:value-of select="3 + 4"/>
<xsl:value-of select="'Hello world'"/>
<xsl:value-of select="substring('Adlib', 1, 2)"/>
<xsl:value-of select="title[1]"/>
</td>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
26. Nu in het echt...
<xsl:template match="record">
<tr class="record">
<td>
<xsl:apply-templates select="priref"/>
</td>
<td>
<xsl:apply-templates select="creator[1]"/>
</td>
<td>
<xsl:apply-templates select="title[1]"/>"
</td>
</tr>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
27. A touch of class
<xsl:template match="record">
<tr class="record">
<td class="priref">
<xsl:apply-templates select="priref"/>
</td>
<td class="creator">
<xsl:apply-templates select="creator[1]"/>
</td>
<td class="title">
<xsl:apply-templates select="title[1]"/>"
</td>
</tr>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
28. Plaatje er bij?
<td class="title">
<xsl:apply-templates select="title[1]"/>"
</td>
<td class ="image">
<xsl:apply-templates select="reproduction.reference[1]"/
>
</td>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
29. Plaatje er bij?
<xsl:template match="reproduction.reference">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="concat('C:Model application 3.5 - Sql
serverimages', .)"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
30. Plaatje te groot?
<xsl:template match="reproduction.reference">
<xsl:element name="img">
<xsl:attribute name="width">100</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="concat('C:Model application
3.5 - Sql serverimages', .)"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
31. Namen omdraaien?
<xsl:template match="creator">
<xsl:choose>
<xsl:when test="contains(., ',')">
<xsl:value-of select="substring-after(., ',')"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-before(., ',')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
32. “Duurzaam programmeren”
<xsl:template match="creator"> Maak er een
<xsl:call-template name="names"> subroutine van voor
<xsl:with-param name="name" select="."/>
</xsl:call-template>
hergebruik
</xsl:template>
<xsl:template name="names">
<xsl:param name="name"/>
<xsl:choose>
<xsl:when test="contains($name, ',')">
<xsl:value-of select="substring-after($name, ',')"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-before($name, ',')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
33. Herhaalde groep
<xsl:template match="dimension.value">
<xsl:variable name="pos" select="position()"/>
<xsl:if test="$pos != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="../dimension.type[$pos]"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="../dimension.unit[$pos]"/>
</xsl:template>
Het gebruik van XSLT in Adlib – workshop 30 maart 2010
34. Good practice
• Combineer XSLT met CSS
• XSLT voor de data transformatie en CSS voor
de opmaak details
• Hou templates klein (altijd kleiner dan 1
pagina)
• Gebruik XSLT niet als programmeertaal, maar
als pattern matching taal
• Vermijd <xsl:for-each> loops
Het gebruik van XSLT in Adlib – workshop 30 maart 2010