The Basics of XSLT Assuming a basic knowledge
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 8 July 2005 Slavic Digital Text Workshop 1
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 8 July 2005 Slavic Digital Text Workshop 2
Why Transform XML • Separating data from presentation – Transform TEI or EAD into HTML for display – Transform MARCXML into HTML for display – Transform Doc. Book 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 8 July 2005 Slavic Digital Text Workshop 3
Hello World! Input http: //www. softwareag. com/xml/Techn_Links/xslt_book/31290103. htm <? xml version="1. 0" encoding="UTF-8"? > <greeting>Hello, world!</greeting> 8 July 2005 Slavic Digital Text Workshop 4
Hello World! Output <html> <head> <title>Today's greeting</title> </head> <body> <p>Hello, world!</p> </body> </html> 8 July 2005 Slavic Digital Text Workshop 5
Hello World! XSLT Stylesheet <? xml version="1. 0" encoding="UTF-8"? > <xsl: stylesheet version="1. 0" xmlns: xsl= "http: //www. w 3. 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> 8 July 2005 Slavic Digital Text Workshop 6
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… 8 July 2005 Slavic Digital Text Workshop 7
Detailed Tutorials from ZVON • XPath • XSLT 8 July 2005 Slavic Digital Text Workshop 8
References • http: //www. w 3. org/Style/XSL/ • http: //www. softwareag. com/xml/Techn_Links/xslt_bo ok/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? fre etext=xslt&product. ID=&Display. Lang=en • http: //www. jenitennison. com/xslt/index. html 8 July 2005 Slavic Digital Text Workshop 9
‘Quick and Dirty’ Rendering with XSLT and CSS and a little Java. Script 8 July 2005 Slavic Digital Text Workshop 10
XSLT Where Should It Happen • Client-side – IE 5+ 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) 8 July 2005 Slavic Digital Text Workshop 11
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 8 July 2005 Slavic Digital Text Workshop 12
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} 8 July 2005 Slavic Digital Text Workshop 13
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> 8 July 2005 Slavic Digital Text Workshop 14
How We Use XSLT (cont. ) • Some elements need their children rearranged i. e. <resp. Stmt><name>Habing</name><resp>Coder</resp></resp Stmt> becomes Coder: Habing <xsl: template match=“resp. Stmt”> <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 8 July 2005 Slavic Digital Text Workshop 15
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. 8 July 2005 Slavic Digital Text Workshop 16
- Slides: 16