160 likes | 203 Views
The Basics of XSLT Assuming a basic knowledge of XML and XML Namespaces. Thomas G. Habing Grainger Engineering Library Information Center University of Illinois at Urbana-Champaign thabing@uiuc.edu. What is XSLT.
E N D
The Basics of XSLTAssuming a basic knowledge of XML and XML Namespaces Thomas G. Habing Grainger Engineering Library Information Center University of Illinois at Urbana-Champaign thabing@uiuc.edu Slavic Digital Text Workshop
What is XSLT • “language for transforming XML documents into other XML documents” plus HTML or even plain text • Version 1.0 Released 1999. • Widely supported by many tools and applications. • Working Drafts for version 2.0 • Associated with XSL-FO, but different • Uses XML syntax • Pattern based • Template rules (based on XPath) indicate how different elements should be processed • For advanced users: Side-effect free, functional programming language Slavic Digital Text Workshop
Why Transform XML • Separating data from presentation • Transform TEI or EAD into HTML for display • Transform MARCXML into HTML for display • Transform DocBook into XSL-FO and then into PDF • Transmitting data between applications • Metadata crosswalks, convert FGDC into MARC • Convert TEI header elements in simple Dublin Core • Extract EAD header into a CSV for import into a database Slavic Digital Text Workshop
Hello World! Inputhttp://www.softwareag.com/xml/Techn_Links/xslt_book/31290103.htm <?xml version="1.0" encoding="UTF-8"?> <greeting>Hello, world!</greeting> Slavic Digital Text Workshop
Hello World! Output <html> <head> <title>Today's greeting</title> </head> <body> <p>Hello, world!</p> </body> </html> Slavic Digital Text Workshop
Hello World! XSLT Stylesheet <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:output method="html" encoding="UTF-8"/> <html> <head> <title>Today's greeting</title> </head> <body> <p><xsl:value-of select="greeting"/></p> </body> </html> </xsl:template> </xsl:stylesheet> Slavic Digital Text Workshop
Hello World! Make it happen • Command line: • java -jar saxon.jar hello.xml hello.xsl > hello.html • msxsl hello.xml hello.xsl -o hello.html • Processing instruction to the client: • <?xml-stylesheet type="text/xsl“ href="hello.xsl"?> • XML Editor • Oxygen, etc. • Programmatically • Some demos go here… Slavic Digital Text Workshop
Detailed Tutorials from ZVON • XPath • XSLT Slavic Digital Text Workshop
References • http://www.w3.org/Style/XSL/ • http://www.softwareag.com/xml/Techn_Links/xslt_book/contents.htm • http://saxon.sourceforge.net/ • http://www.zvon.org/o_html/group_xsl.html • http://www.topxml.com/xsl/default.asp • http://www.topxml.com/xsl/articles/fp/ • http://www.microsoft.com/downloads/results.aspx?freetext=xslt&productID=&DisplayLang=en • http://www.jenitennison.com/xslt/index.html Slavic Digital Text Workshop
‘Quick and Dirty’ Rendering with XSLT and CSS and a little JavaScript Slavic Digital Text Workshop
XSLT Where Should It Happen • Client-side • IE5+ and Mozilla (Netscape 7+) • Can reduce the load on your servers • But performance on low-end clients can be BAD • Server-side • Performance could be a problem on busy servers, serving large, complex documents • More control and flexibility over the conversion (metamerge) • Offline Preconversion • Best performance • Not good for dynamic documents (metamerge) Slavic Digital Text Workshop
How We Use XSLT • Most tags are converted to HTML spans with a class attribute equal to the original tag name,i.e. <title>becomes<span class=“title”> <xsl:template match=“*”> <xsl:element name=“span”> <xsl:attribute name=“class”> <xsl:value-of select=“local-name()”/> </xsl:attribute> <xsl:apply-templates/> </xsl:element></xsl:template> • If the tag must be a block element, that is specified in the CSS: span.title {display:block} • This improves the modularity of the code Slavic Digital Text Workshop
How We Use XSLT (cont.) • Some tags require special conversion,i.e.<title rend=“bold”>becomes <span class=“title_rend_bold”> <xsl:template match=“title”> <xsl:element name=“span”> <xsl:attribute name=“class”> <xsl:value-of select=“local-name()”/> <xsl:text>_rend_</xsl:text> <xsl:value-of select=“@type”/> </xsl:attribute> <xsl:apply-templates/> </xsl:element></xsl:template> • CSS: span.title_rend_ital {font-style:italic} span.title_rend_bold {font-weight:bold} Slavic Digital Text Workshop
How We Use XSLT (cont.) • Some elements need to have punctuation added, i.e.<list><item>Nikolai</item><item>Vasilli</item><item>Bob</item></list> becomes Nikolai, Vasilli, Bob. <xsl:template match=“list/item[position() = last()]”> <span><xsl:call-template name=“class-attr”/> <xsl:apply-templates/><xsl:text>.</xsl:text> </span></xsl:template><xsl:template match=“list/item[position() != last()]”> <span><xsl:call-template name=“class-attr”/> <xsl:apply-templates/> <xsl:text>, </xsl:text> </span></xsl:template> Slavic Digital Text Workshop
How We Use XSLT (cont.) • Some elements need their children rearrangedi.e.<respStmt><name>Habing</name><resp>Coder</resp></respStmt> becomes Coder: Habing <xsl:template match=“respStmt”> <span><xsl:call-template name=“class-attr”/> <xsl:apply-templates select=“resp”/> <xsl:text>: </xsl:text> <xsl:apply-templates select=“name”/> </span></xsl:template> • ‘Real’ DTDs can require some fairly complex processing. • So far XSLT seems to be able to handle nearly every case we have come across • However, some cases have required extensions to XSLT Slavic Digital Text Workshop
How We Use XSLT (cont.) • Some elements are converted into HTML elements other than <span> • Elements representing figures or images are converted to the <img src=“…”> tag. • Elements representing internal links with ID and IDREF attributes are usually converted into <a name=“…”></a> or <a href=“#…”>…</a> tags. • Table elements are converted into corresponding HTML <table>, <tr>, or <td> tags. • In all cases, a class attribute is added which is the original XML tag name; this is used in the CSS. Slavic Digital Text Workshop