university of dublin trinity college 4d2b...university of dublin trinity college 4d2b –...

31
University of Dublin Trinity College 4D2b – Transforming XML Documents [email protected]

Upload: others

Post on 17-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

University of Dublin Trinity College

4D2b – Transforming XML Documents

[email protected]

Page 2: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

What is XSL? •  eXtensible Stylesheet Language consists of

–  A language for transforming XML documents –  A language for specifying formatting properties

•  Based on existing style sheets languages –  Document Style Semantics and Specification Language

(DSSSL) –  Cascading Style Sheets (CSS)

•  A style sheet language specifically for XML documents •  Uses an XML syntax •  XSL is a combination of three specifications

–  XML Path Language (XPath) –  XSL Transformations (XSLT) –  XSL-FO (Formatting Objects)

More @ http://www.w3.org/Style/XSL/

Page 3: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

What are XSL Transformations?

•  Language for transforming XML documents into other XML/other documents – More generally input can be any document as long

as it is represented as tree •  Uses a collection of templates (rules) to

transform the source document •  XPath is used to select the parts of a source

document to transform •  Server-side/client-side execution

Page 4: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Why XSL Transformations?

•  Powerful transformation functionality •  An XML document may be completely

restructured – E.g. into an SQL Create table/Insert script

•  Good software support for transformations •  Easy and fast prototyping possible

Page 5: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

XSLT Processing

XML Doc

Style/Transform Sheet

Result Doc

Transformation Process

Page 6: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Nodes in a Tree Model

Node

Root Element

Text

Attribute Namespace

Comment Processing Instruction

Page 7: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Example Tree

root

Text Smith

element firstname

Element surname

Text John

Text is generic

<firstname> John </firstname> <surname> Smith </surname> is generic

Page 8: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Fundamental Notion of xslt:template

<xsl:template match=“firstname">

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

</xsl:template>

Data nodes

Pattern to match

Instruction

•  Elements in a template body classified as either data node or instruction

•  When the template is executed – Data nodes get copied to the result tree –  Instructions are executed

<firstname> John </firstname>

<h2> John </h2>

element firstname

Text John

Text </h2>

Text John

Text <h2>

Page 9: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Gaining control of the processing •  Built in template rule for element node

– <xsl:apply-templates/> –  “select all children of the current node in the

source tree and for each one find the matching template rule in the stylesheet and execute it”

•  Controlling the sequence of processing – Explicitly use instructions <xsl:apply-templates/>

or <xsl:apply-templates select=“some_pattern”> – Use <xsl:call-template> to invoke a specific

template by name rather than by pattern matching

Page 10: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Gaining control of the processing •  Built in template rule for text node

– Copy text node to output •  Extracting required information from node

directly – <xsl:value-of select=“some_pattern”/>

Page 11: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Conflict Resolution Policy •  What if more than one template rule whose

pattern matches particular node? •  Conflict can be resolved…

– Current stylesheet rules take precedence over imported stylesheet rules

– By assigned priority through template definition or system allocation

–  In the case where same import precedence and same priorities… down to the way the XSLT engine is implemented!

Page 12: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Selection 1.  xsl:stylesheet 2.  xsl:transform 3.  xsl:output 4.  xsl:template 5.  xsl:apply-templates 6.  xsl:value-of 7.  xsl:sort 8.  xsl:text 9.  xsl:attribute 10. xsl:element 11. xsl:variable

12. xsl:if 13. xsl:choose 14. xsl:when 15. xsl:otherwise 16. xsl:param 17. xsl:with-param 18. xsl:call-template

Page 13: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

xsl:transform, xsl:stylesheet <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> [...] </xsl:transform>

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> [...] </xsl:stylesheet>

Page 14: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

xsl:output <?xml version="1.0"?> <xsl:stylesheet

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

<xsl:output

method="html" indent="yes"/>

[...] </xsl:stylesheet>

<?xml version="1.0"?> <xsl:stylesheet

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

<xsl:output

method="xml"/> [...] </xsl:stylesheet>

Page 15: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

xsl:template <xsl:template match="/"> </xsl:template> <xsl:template match="name"> </xsl:template> <xsl:template match="//age"> </xsl:template> <xsl:template match="p/name"> </xsl:template> <xsl:template match="p[age='21']"> </xsl:template> <xsl:template match="p[@sex='f']"> </xsl:template> <xsl:template match= "name[ancestor::grp/@id='G4']"> </xsl:template>

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

XML Source XSLT

Page 16: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:apply-templates <xsl:template match="/"> <xsl:apply-templates select="grp"/> </xsl:template> <xsl:template match="grp"> <xsl:apply-templates select="p"/> </xsl:template> <xsl:template match="p"> <xsl:apply-templates /> </xsl:template> <xsl:template match="name"> </xsl:template> <xsl:template match="age">

</xsl:template>

XML Source XSLT

Page 17: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:value-of <xsl:template match="/"> <xsl:apply-templates select="grp"/> </xsl:template> <xsl:template match="grp"> <ul> <xsl:apply-templates select="p"/> </ul> </xsl:template> <xsl:template match="p"> <li> <xsl:value-of select="name"/> <xsl:apply-templates select="age" /> </li> </xsl:template> <xsl:template match="age"> <xsl:value-of select="."/> </xsl:template>

<ul> <li>Mike 24</li> <li>Tanja 21</li> </ul>

XML Source XSLT

Result

Page 18: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

© 2003 B. Jung

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:text & text() <xsl:template match="/"> <xsl:apply-templates

select="grp"/> </xsl:template> <xsl:template match="grp"> <ul> <xsl:apply-templates select="p/name"/> </ul> </xsl:template> <xsl:template match="name"> <li> <xsl:text>Name: </xsl:text> <xsl:apply-templates /> </li> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template>

<ul> <li>Name: Mike</li> <li>Name: Tanja</li> </ul>

XML Source XSLT

Result

Page 19: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

© 2003 B. Jung

<database> <person age='34'>

<name> <title> Mr </title> <firstname> John </firstname> <firstname> Paul </firstname> <surname> Murphy </surname> </name> <hobby> Football </hobby> <hobby> Racing </hobby>

</person> <person >

<name> <firstname> Mary </firstname> <surname> Donnelly </surname> </name>

</person> </database>

•  Stylesheet print out student surname (bolded) and any hobbys

Page 20: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet
Page 21: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

xsl:call-template <xsl:template match="grp"> <div> <xsl:apply-templates /> </div> </xsl:template> <xsl:template match="p"> <dt> <xsl:value-of select="name" /> </dt> <dd> <xsl:value-of select="age" /> <xsl:call-template name="d" /> </dd> </xsl:template> <xsl:template name="d"> <xsl:text>(more than </xsl:text> <xsl:value-of select="365*number(age)" /> <xsl:text> days)</xsl:text> </xsl:template>

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

<dl> <dt>Mike</dt> <dd>24 (more than 8760 days)</dd> <dt>Tanja</dt> <dd>24 (more than 7665 days)</dd> </dl>

XML Source XSLT

Result

Page 22: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

xsl:sort <xsl:template match="grp"> <dl> <xsl:apply-templates select="p"> <xsl:sort select="age" /> </xsl:apply-templates> </dl> </xsl:template> <xsl:template match="p"> <dt> <xsl:value-of select="name" /> </dt> <dd> <xsl:value-of select="age" /> <xsl:text> years old</xsl:text> </dd> </xsl:template>

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

<dl> <dt>Tanja</dt> <dd>21 years old</dd> <dt>Mike</dt> <dd>24 years old</dd> </ul>

XML Source XSLT

Result

Page 23: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

Transformation into HTML <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="//p" /> </body> </html> </xsl:template> <xsl:template match="p"> <h1> <xsl:value-of select="name" /> </h1> </xsl:template>>

<html> <body> <h1>Mike</h1> <h1>Tanja</h1> </body> </html>

XML Source XSLT

Result

Page 24: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:element <xsl:template match='grp'> <table> <xsl:apply-templates /> </table> </xsl:template> <xsl:template match="p"> <xsl:element name="tr"> <xsl:apply-templates /> </xsl:element> </xsl:template> <xsl:template match="name|age"> <td> <xsl:value-of select="." /> </td> </xsl:template>

<table> <tr> <td>Mike</td> <td>24</td> </tr> <tr> <td>Tanja</td> <td>21</td> </tr> </table>

XML Source XSLT

Result

Page 25: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:attribute <xsl:template match="grp"> <table> <xsl:apply-templates /> </table> </xsl:template> <xsl:template match="p"> <xsl:element name="tr"> <xsl:apply-templates /> </xsl:element> </xsl:template> <xsl:template match="name|age"> <td> <xsl:attribute name="class"> <xsl:value-of select="local-name()" /> </xsl:attribute> <xsl:value-of select="." /> </td> </xsl:template>

<table> <tr> <td class="name">Mike</td> <td class="age">24</td> </tr> <tr> <td class="name">Tanja</td> <td class="age">21</td> </tr> </table>

XML Source XSLT

Result

Page 26: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:variable <xsl:template match="grp"> <ul> <xsl:apply-templates select="p"/> </ul> </xsl:template> <xsl:template match="p"> <li> <xsl:variable name="a" select= "age"/> <xsl:variable name="n" select= "name" /> <xsl:value-of select="$a" /> <xsl:value-of select="$n" /> </li> </xsl:template>

<ul> <li>Mike 24</li> <li>Tanja 21</li> </ul>

XML Source XSLT

Result

Page 27: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:param, -with-param <xsl:template match="grp"> <ul> <xsl:apply-templates /> </ul> </xsl:template> <xsl:template match="p"> <li> <xsl:apply-templates select="name"> <xsl:with-param name="a"> <xsl:value-of select="age"/> </xsl:with-param> </xsl:apply-templates> </li> </xsl:template> <xsl:template match="name"> <xsl:param name="a" /> <xsl:value-of select="."/> <xsl:value-of select="$a"/> </xsl:template>

<ul> <li>Mike 24</li> <li>Tanja 21</li> </ul>

XML Source XSLT

Result

Page 28: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:if <xsl:template match="grp"> <xsl:apply-templates /> </xsl:template> <xsl:template match="p"> <div> <xsl:attribute name="class"> <xsl:if test="@sex='m'"> <xsl:text>male</xsl:text> </xsl:if> <xsl:if test="@sex='f'"> <xsl:text>female</xsl:text> </xsl:if> </xsl:attribute> <xsl:apply-templates /> </div> </xsl:template> <xsl:template match="name"> <xsl:value-of select="." /> <xsl:value-of select="../age" /> </xsl:template>

<div class="male"> Mike 24 </div> <div class="female"> Tanja 21 </div>

XML Source XSLT

Result

Page 29: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

<?xml version='1.0'?> <grp id="G4"> <p id="P3" sex="m"> <name>Mike</name> <age>24</age> </p> <p id="P7" sex="f"> <name>Tanja</name> <age>21</age> </p> </grp>

xsl:choose, xsl:when, xsl:otherwise <xsl:template match="p"> <div> <xsl:attribute name="class"> <xsl:choose> <xsl:when test="age &lt; 10"> <xsl:text>child</xsl:text> </xsl:when> <xsl:when test="age &lt; 20"> <xsl:text>teen</xsl:text> </xsl:when> <xsl:when test="age &lt; 30"> <xsl:text>twenties</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>adult</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> <xsl:value-of select="name" /> </div> </xsl:template>

<div class="twenties"> Mike </div> <div class="twenties"> Tanja </div>

XML Source XSLT

Result

Page 30: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

XSLT Example <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="/"> <html> <head><title><xsl:apply-templates select="doc/title"/></title></head> <body> <xsl:attribute name="id"> <xsl:value-of select="doc/@isbn"/> </xsl:attribute> <h1><xsl:value-of select="doc/author” /></h1> <xsl:apply-templates select="//chapter"/> </body> </html> </xsl:template> <xsl:template match="chapter"> <h2><xsl:apply-templates select="*"/></h2> </xsl:template> <xsl:template match="title"> <xsl:value-of select= "."/> </xsl:template> </xsl:stylesheet>

Page 31: University of Dublin Trinity College 4D2b...University of Dublin Trinity College 4D2b – Transforming XML Documents Owen.Conlan@scss.tcd.ie What is XSL? • eXtensible Stylesheet

Summary •  Selects (a set of) ELEMENTs within an XML

document based on – Conditions – Hierarchy

•  Usage – Retrieving info from a single XML document – Applying XSL style sheet rules – Making XQuerys