XSLT Style Sheet and root template

XSL Style Sheet

An Extensible Stylesheet Language (XSL) style sheet can be applied to the query results. When you execute a template using HTTP, you can specify an XSL file in these ways:

  • Use the sql:xsl attribute in the template.
  • Use the xsl keyword as part of the URL to specify the XSL file that will be used to process the resulting XML data.

If the XSL file is specified both in the template using sql:xsl and in the URL using the keyword xsl, the XSL style sheet specified in the template is applied to the results first, and then the XSL file specified in the URL is applied.

Examples:

In the following example, nwind is a virtual directory created using the IIS Virtual Directory Management for SQL Server utility, and template is the virtual name of template type defined when the virtual directory is created (any name can be given to a virtual name when it is created). For more information, see Using IIS Virtual Directory Management for SQL Server Utility.
A. Specify sql:xsl in a template to process the result
In this example, a template includes a simple SELECT statement. The query result is processed according to the instructions in the XSL file specified using sql:xsl.
<?xml version =’1.0′ encoding=’UTF-8′?>
<root xmlns:sql=’urn:schemas-microsoft-com:xml-sql’
sql:xsl=’MyXSL.xsl’>
<sql:query>
SELECT FirstName, LastName FROM Employees FOR XML AUTO
</sql:query>
</root>
For illustration purposes, the template (TemplateWithXSL.xml) is stored in the directory associated with the virtual nameFirstName (template), of template type. The XSL file (MyXSL.xsl) is also stored in the same directory.
This is the XSL file:
<?xml version=’1.0′ encoding=’UTF-8′?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″>
<xsl:template match = ‘Employees’>
<TR>
<TD><xsl:value-of select = ‘@FirstName’ /></TD>
<TD><B><xsl:value-of select = ‘@LastName’ /></B></TD>
</TR>
</xsl:template>
<xsl:template match = ‘/’>
<HTML>
<HEAD>
<STYLE>th { background-color: #CCCCCC }</STYLE>
</HEAD>
<BODY>
<TABLE border=’1′ style=’width:300;’>
<TR><TH colspan=’2′>Employees</TH></TR>
<TR><TH >First name</TH><TH>Last name</TH></TR>
<xsl:apply-templates select = ‘root’ />
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Share this post
[social_warfare]
Elements, Attributes and values
Looping and Conditional processing

Get industry recognized certification – Contact us

keyboard_arrow_up