220 likes | 408 Views
Databases and Information Systems 1. Transformation of XML Documents. TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A. XSLT. Extensible Stylesheet Language (XSL) Extensible Stylesheet Language Transformations (XSLT). XML. XML Document. (X)HTML.
E N D
DatabasesandInformation Systems 1 Transformation of XML Documents TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A
XSLT • Extensible Stylesheet Language (XSL) • Extensible StylesheetLanguage Transformations (XSLT) XML XML Document (X)HTML XSLT Processor XSL Stylesheet Text
Obtaining an XSLT IDE • XML Cooktop • http://www.xmlcooktop.com/
XSL defaulttemplates 1/2 • Default template for elements and root: <xsl:templatematch = "*|/"> <xsl:apply-templates/></xsl:template> • Default template for text nodes and attributes: <xsl:templatematch="text()|@*"> <xsl:value-ofselect="."/> </xsl:template>
XSL defaulttemplates 2/2 • Default templateforcommentsandprocessinginstructions: <xsl:templatematch="comment()|processing-instruction()"></xsl:template> • defaultbehaviourfornamespacenodesdo not outputnamespacenodes
ProcessingModel <xsl:templatematch="/DB"> <xsl:apply-templatesselect="order"/> </xsl:template> <xsl:templatematch="order"> <xsl:value-ofselect="@ID"/> <xsl:apply-templatesselect="*"/> </xsl:template> <xsl:templatematch="customer|PC"> <xsl:value-ofselect="."/> </xsl:template> <xsl:templatematch="/ | ∗"> <xsl:apply-templates/> </xsl:template> / DB order order custo-mer PC PC custo-mer Node list: <order, order> <customer, PC> <order> <PC> <root> <customer, PC> <PC> Output: 23 Meier Pc500 24 Reich pc600
Template Match v’ P v = actual node • Whendoes a templatematch? • Letv betheactualcontextnode. LetPbe an XPathexpressionwithinthematchattributeofthe tag <xsl:templates> • Pmatches,itexists an ancestornodev’(v=v’ ok), so thatvistheresultofevaluatingPwiththeactualnodev’.
HTML file and HTML output <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>
XSLT stylesheet and HTML output 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 </table> </body> </html> separate template
XPath location expressions in XSL Application in XSL:XPath location expression <xsl:template match="Auftrag"> <xsl:value-of select="PC"> <xsl:if test='position()>1'>...</xsl:if> </xsl:template> XPath Boolean expression
More locationexpressions (examples) • locationexpression in XSL: „outputattributesize“ • <xsl:value-ofselect="@size"/> • XPath Boolean expressions „ifattribute type hasthevalueCHAR , output ... “ • <xsl:iftest="@type='CHAR'„> (<xsl:value-ofselect="@size"/>)</xsl:if> • „ifcurrentis not firstwithinparent (position >1):“ • <xsl:iftest='position() > 1'> not thefirst</xsl:if>
CurrentNode ContextNode • current() returns a node-set that contains only the currentnode. • Usually the currentnodeand the contextnode (.) are the same. • But… Current node Context node
Generating an SQL script file with XSLT <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> XSLT DB
Advantages of XML and XSL • Generate HTML on a web server: • transform: data.xml + layout.xsl -> x.html • XML istransformableby different XSL filestogenerateappropriateoutputfor different destinationformats, e.g. html , pdf , txt, …
Summary • Database XML PDF or HTML or TXT or XML or… • Database XML SQL Database DB XSLT Original database DB Other database