The Structure of a Stylesheet |
• XSLT Stylesheets are XML documents; namespaces (http://www.w3.org/TR/REC-xml-names) are used to identify semantically significant elements. |
• Most stylesheets are stand-alone documents rooted at <xsl:stylesheet> or <xsl:transform>. It is possible to have “single template” stylesheet/documents. |
• <xsl:stylesheet> and <xsl:transform> are completely synonymous. |
Note that it is the mapping from namespace abbreviation to URI that is important, not the literal namespace abbreviation “xsl:” that is used most commonly. |
A Stylesheet |
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″> … </xsl:stylesheet> |
A Transformation Sheet |
<eg:transform xmlns:eg=”http://www.w3.org/1999/XSL/Transform” version=”1.0″> … </eg:transform> |
Document as Stylesheet |
<html xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <head> <title>Silly Example</title> </head> <body> <h1>Silly Example</h1> <p>You’d probably use extension elements, or somthing more interesting in real life: 3+4 is <xsl:value-of select=”3+4″/>. </p> </body> </html> |
XML document for city entity |
<?xml version="1.0" encoding="UTF-8"?> <!-- Document: city.xml --> <cities xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='host.xsd'> <city> <cityID>c1</cityID> <cityName>Atlanta</cityName> <cityCountry>USA</cityCountry> <cityPop>4000000</cityPop> <cityHostYr>1996</cityHostYr> </city> .......... </cities> |
XML stylesheet for city entity |
<?xml version="1.0" encoding="UTF-8"?> <!-- Document: city2.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="utf-8" indent="yes" /> <xsl:attribute-set name="date"> <xsl:attribute name="year">2004</xsl:attribute> <xsl:attribute name="month">03</xsl:attribute> <xsl:attribute name="day">19</xsl:attribute> </xsl:attribute-set> <xsl:template match="tourGuide"> <xsl:processing-instruction name="xsl-stylesheet"> href="style.css" type="text/css"<br /> </xsl:processing-instruction> <xsl:comment>This is a list of the cities we are visiting this week</xsl:comment> <xsl:for-each select="city"> <!-- element name creates a new element where the value of the attribute <tt>name</tt> sets name of the new element. Multiple attribute sets can be used in the same element --> <!-- use-attribute-sets attribute adds all the attributes declared in attribute-set from above --> <xsl:element name="cityList" use-attribute-sets="date"> <xsl:element name="city"> <xsl:attribute name="country"> <xsl:apply-templates select="country"/> </xsl:attribute> <xsl:apply-templates select="cityName"/> </xsl:element> <xsl:element name="details">Will write up a one page report of the trip</xsl:element> </xsl:element> </xsl:for-each> </xsl:template> </xsl:stylesheet> |
• Although the output method is set to “xml“, since there is no <html> element as the root of the result tree, it would default to XML output. |
• attribute-set is a top-level element that creates a group of attributes by the name of “date.” This attribute set can be reused throughout the stylesheet. The element attribute-set also has the attribute use-attribute-sets allowing you to chain together several sets of attributes. |
• The processing-instructionproduces the XML stylesheet processing instructions. |
• The element comment creates a comment in the result tree |
• The attribute element allows you to add an attribute to an element that is created in the result tree. |