xslt overview
play

XSLT: Overview XSLT 1.0 (W3C Rec. 11/1999; XSLT uses XML syntax for - PDF document

3 Document Transformations XSLT: Overview XSLT 1.0 (W3C Rec. 11/1999; XSLT uses XML syntax for expressing XSLT 2.0 Candidate Rec. 11/05) transformations A language for transforming XML documents of a document source tree into a


  1. 3 Document Transformations XSLT: Overview � XSLT 1.0 (W3C Rec. 11/1999; � XSLT uses XML syntax for expressing XSLT 2.0 Candidate Rec. 11/05) transformations – A language for transforming XML documents – of a document source tree into a result tree – initial main purpose to support XSL formatting » result and source are separate trees – currently mainly (?) used as an independent transformation language (esp. XML → HTML) – by template rules � Our goal: to understand the basic model and � Each template rule has central features of XSLT – a pattern (matched against nodes of the source tree) – Overview and an example – a template as a body – Data model and processing model » instantiated to create fragments of the result tree XPT 2006 Overview of XSLT 1 XPT 2006 Overview of XSLT 2 Overview of XSLT Transformation Style Sheets and Template Rules Style � An xsl:stylesheet (or xsl:transform ) Sheet Output Process consists of template rules: conventional XSLT <xsl:template match=" Pattern "> namespace prefix Template <!-- NB: well-formed! --> XML </xsl:template> � Rule applied to nodes of the source tree matched Result Tree Source Source Tree by the Pattern HTML Document – expressed using XPath (XML Path Language) � Template consists of Text » literal result tree fragments (elements, text), and Transformation Process » XSLT instructions for creating further result tree fragments XPT 2006 Overview of XSLT 3 XPT 2006 Overview of XSLT 4 XPath in a Nutshell An XSL transformation example � Transform below document to HTML: � XPath 1.0 W3C Rec. 11/99 (2.0 Cand.Rec. 11/05) – a compact non-XML syntax for addressing parts of <?xml-stylesheet type="text/xsl" href="walsh.xsl" ?> XML documents (as node-sets ) <!-- Modified from an example by Norman Walsh --> – used also in other W3C languages <doc><title>My Document</title> » Specs for hyperlinks in XML: <para>This is a <em>short</em> document.</para> XLink (Rec. '01) and XPointer (Rec. '03) <para>It only exists to <em>demonstrate a » XQuery (WD, Sept '05; extends XPath 2.0) <em>simple</em> XML document</em>.</para> – also typical operations on strings, numbers and truth <figure><title>My Figure</title> values <graphic fileref="myfig.jpg"/></figure> </doc> XPT 2006 Overview of XSLT 5 XPT 2006 Overview of XSLT 6 Result (edited for readability) Example style sheet begins <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 <xsl:stylesheet version="1.0" Transitional//EN"> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <xsl:template match=" / "> <!-- rule for root --> <BODY> <H1>My Document</H1> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <P>This is a <I>short</I> document.</P> <BODY> <P>It only exists to <I>demonstrate a <B>simple</B> XML <!-- process root's children here: --> document</I>.</P> <xsl:apply-templates /> <DIV> </BODY> </HTML> <B>Figure 1. </B> <BR> </xsl:template> <IMG src="myfig.jpg"><B>My Figure</B> </DIV> <xsl:template match=" doc/title "> </BODY> <H1><xsl:apply-templates /></H1> </HTML> </xsl:template> XPT 2006 Overview of XSLT 7 XPT 2006 Overview of XSLT 8

  2. Example ( para s and emph s) Example ( figure s) <xsl:template match=" figure "> <!-- Insert a bold caption of form ' Figure Num. ' by counting all figures in the document: --> <xsl:template match=" para "> <DIV><B>Figure <xsl:number level="any" <P><xsl:apply-templates /></P> count="figure"/>. </B> </xsl:template> <BR /> <!-- Process the children of figure, --> <xsl:template match=" em "> <!-- the 'graphic' child first: --> <I><xsl:apply-templates /></I> <xsl:apply-templates select=" graphic " /> </xsl:template> <xsl:template match=" em/em "> <!-- then the 'title' child: --> <B><xsl:apply-templates /></B> <xsl:apply-templates select=" title " /> </xsl:template> </DIV> </xsl:template> XPT 2006 Overview of XSLT 9 XPT 2006 Overview of XSLT 10 Example (end of style sheet) Result (edited for readability) <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 <xsl:template match=" graphic "> Transitional//EN"> <IMG src="{ @fileref }" /> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <!-- Assign the value of attribute <BODY> <H1>My Document</H1> 'fileref' to attribute 'src' --> <P>This is a <I>short</I> document.</P> </xsl:template> <P>It only exists to <I>demonstrate a <B>simple</B> XML document</I>.</P> <xsl:template match=" figure/title "> <DIV> <B> <xsl:apply-templates /> </B> <B>Figure 1. </B> <BR> </xsl:template> <IMG src="myfig.jpg"><B>My Figure</B> </DIV> </xsl:stylesheet> </BODY> </HTML> XPT 2006 Overview of XSLT 11 XPT 2006 Overview of XSLT 12 What use of XSL(T)? XSLT in online document delivery � XSL can be used in different ways � XSLT in a browser – for offline document formatting – defines rendering of XML documents » produce, say, PDF from XML by an XSL style – supported by MS IE, and Netscape/Mozilla (7.0/1.7) sheet (using XSLT + XSL formatting objects ) » transformation of XML to HTML on the fly in browser » NB : Microsoft's implementation used to differ from XSLT 1.0 – for offline document manipulation � XSLT on a Web server » transform XML into other form (XML/HTML/text) using XSLT – an HTTP request served by transforming XML on the fly to HTML (or other format) on the server – for online document delivery » on a Web server » in a Web browser (if the browser supports) XPT 2006 Overview of XSLT 13 XPT 2006 Overview of XSLT 14 Main Aspects of XSLT Data Model of XSLT and XPath � Documents are viewed as trees � Data model made of seven types of nodes: – How is document data viewed in XSLT? – root (additional parent of document element) � Selection mechanism – element nodes – How are document parts selected for processing? – attribute nodes – text nodes � Matching – comments , processing instructions and – How are the template rules selected? namespaces � Processing model � NB : Entity references are expanded – How does the XSLT execution proceed? −> no entity nodes XPT 2006 Overview of XSLT 15 XPT 2006 Overview of XSLT 16

Recommend


More recommend