Transformation of XML Documents Extensible Stylesheet Language (XSL) Extensible Stylesheet Language Transformations (XSLT) XML XML Document (X)HTML XSL Stylesheet Text DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 2/21 XML Cooktop http://www.xmlcooktop.com/ DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 3/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 4/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 5/21 Default template for elements and root: <xsl:template match = "*|/"> <xsl:apply-templates/> </xsl:template> Default template for text nodes and attributes: <xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template> DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 6/21 Default template for comments and processing instructions: <xsl:template match="comment()|processing-instruction()"> </xsl:template> default behaviour for namespace nodes do not output namespace nodes DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 7/21 <xsl:template match="/DB"> <xsl:apply-templates select="order"/> </xsl:template> / DB <xsl:template match="order"> <xsl:value-of select="@ID"/> <xsl:apply-templates select="*"/> </xsl:template> order customer PC order customer PC <xsl:template match="customer|PC"> <xsl:value-of select="."/> Node list: <PC> <order, <root> <customer, <order> <customer, <PC> order> PC> PC> </xsl:template> <xsl:template match="/ | ∗"> <xsl:apply-templates/> </xsl:template> DBIS 1 - 2007 Output: 23 Meier Pc500 24 Reich pc600 Stefan Böttcher and Sebastian Obermeier 8/21 When does a template match? Let v be the actual context node. Let P be an XPath expression within the match attribute of the tag <xsl:templates> P matches , it exists an ancestor node v’ (v=v’ ok), so that v is the result of evaluating P with the actual node v’. DBIS 1 - 2007 v’ P Stefan Böttcher and Sebastian Obermeier v = actual node 9/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 10/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 11/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 12/21 <html> <body> <table width="100%" border="1"> <tr> <td>customer: </td><td>PC : </td> </tr> <tr> <td>Meier</td><td>pc500</td> </tr> <tr> <td>Reich</td><td>pc600</td> </tr> </table> </body> </html> DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 13/21 first Template <html> <body> <table width="100%" border="1"> <tr> <td>customer: </td><td> PC : </td> </tr> start here for every customer node repeat <tr> <td>Name of customer</td><td>PC</td> </tr> until here for every customer node separate template </table> </body> </html> DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 14/21 DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 15/21 Application in XSL: XPath location expression <xsl:template match="Auftrag"> <xsl:value-of select="PC"> <xsl:if test='position() &gt; 1'>...</xsl:if> </xsl:template> XPath Boolean expression DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 16/21 location expression in XSL: „output attribute size“ XPath Boolean expressions „if attribute type has the value CHAR , output ... “ <xsl:value-of select="@size"/> <xsl:if test="@type='CHAR'„> (<xsl:value-of select="@size"/>) </xsl:if> „if current is not first within parent (position >1):“ <xsl:if test='position() &gt; 1'> not the first </xsl:if> DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 17/21 current() returns a node-set that contains only the current node. Usually the current node and the context node (.) are the same. But… Current node Context node DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 18/21 <xsl:template match="/"> create table order( customer char(10) , PC char(10) ) ; <xsl:apply-templates/> </xsl:template> <xsl:template match=„order"> insert into order values( <xsl:value-of select=„customer"/> , <xsl:value-of select="PC"/> ) ; </xsl:template> DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 19/21 Generate HTML on a web server: transform: data.xml + layout.xsl -> x.html XML is transformable by different XSL files to generate appropriate output for different destination formats, e.g. html , pdf , txt, … DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 20/21 Database XML PDF or HTML or TXT or XML or… Database XML SQL Database Original database Other database DBIS 1 - 2007 Stefan Böttcher and Sebastian Obermeier 21/21