Applying Style |
One model for applying style is to allow the process to run recursively, driven primarily by the document. A series of templates is created, such that there is a template to match each context, then these templates are recursively applied starting at the root of the document. |
• <xsl:template> <xsl:template match=”section/title”> <h2><xsl:apply-templates/></h2> </xsl:template> |
• <xsl:apply-templates> <xsl:apply-templates select=”th|td”/> |
There are two obstacles to overcome when using the recursive model, how to arbitrate between multiple patterns that match and how to process the same nodes in different contexts. |
<xsl:stylesheet version=”version_number” id=”unique_id” exclude-result-prefixes=”list” extension-element-prefixes=”list” > |
</xsl:stylesheet> |
The xsl:stylesheet element contains all other XSLT elements and delimits the start and stop of the code in an .xsl program. No other XSLT element can occur before the opening xsl:stylesheet element, nor may any other XSLT element occur after the closing xsl:stylesheet element. |
A direct comparison can be made between the opening and closing <html> … </html> tags of the HTMLlanguage, in that both the html tag and the xsl:stylesheet element serve as delimiting containers for all other members of their respective languages. |
An important duty performed by the xsl:stylesheet element is that it must contain the XSLT namespace declaration. The purpose of the namespace declaration is to declare that the document is an XSLTstylesheet. Use the following syntax: |
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
Older, proprietary versions of Microsoft XSLT used the following namespace: xmlns:xsl=”http://www.w3.org/TR/WD-xsl” |
The xsl:stylesheet element is also commonly referred to as the root element of the stylesheet. Since it is the root, there can be no other element that is a parent to the xsl:stylesheet element. All other XSLTelements are either children, grandchildren, or further descendants. Only the following eight XSLTelements can be children: |
• xsl:attribute-set • xsl:import • xsl:include • xsl:output • xsl:param • xsl:script • xsl:template • xsl:variable |
The xsl:transform element performs the exact same purpose as the xsl:stylesheet element. These two elements may be used interchangeably. They are considered synonymous with each other. |
This is not a self-closing element. The separate closing element is mandatory |
version=”version_number” |
The mandatory version attribute is set to the version number. Currently, the version number is “1.0”. |
id=”unique_id” |
The optional id attribute is a unique identifier for the stylesheet. This allows the stylesheet to be referenced in another XML document. |
exclude-result-prefixes=”list” |
The optional exclude-result-prefixes attribute is a list, delimited by white-space, of the namespaces prefixes that should not be copied into the output (the result tree). |
extension-element-prefixes=”list” |
The optional extension-element-prefixes attribute is a list, delimited by white-space, of the namespaces prefixes used for extension elements. |
Consider the following example: |
<?xml version="1.0" encoding="iso-8859-1"?> <!-- DWXMLSource="sample1.xml" --> <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY copy "©"> <!ENTITY reg "®"> <!ENTITY trade "™"> <!ENTITY mdash "—"> <!ENTITY ldquo "“"> <!ENTITY rdquo "”"> <!ENTITY pound "£"> <!ENTITY yen "¥"> <!ENTITY euro "€"> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="iso-8859-1" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="http://www.w3.org/TR/html4/loose.dtd"/> <xsl:template match="/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>Sample XSL</title> </head> <body> <xsl:for-each select="ebiz/employee_details"> <div style="background-color:#009999; border:thin #000099 solid"> <xsl:value-of select="emp_id"></xsl:value-of><br /> <xsl:value-of select="lname"></xsl:value-of> <span style="color:#000099"><xsl:value-of select="fname">< /xsl:value-of></span><br /> <xsl:value-of select="department"></xsl:value-of><br /> <xsl:value-of select="designation"></xsl:value-of> </div> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> |