Creating XSLT Files
To format notifications, the content formatter uses one or more XSLT files per notification class. You define the XSLT files to transform the intermediate XML data for each notification into a formatted message.
For each notification, the distributor converts the raw notification into an XML document. For all notifications other than digests, the XML document looks like this:
<notifications> <notification> <FieldName>value</FieldName> <FieldName>value</FieldName> ... </notification> </notifications>
The names of the elements within a notification element reflect the names of the notification fields, as specified in the application definition, and their values are the values of the notification fields. The notification data consists of notification fields from the notification table as well as computed notification fields.
For example, the distributor might produce the following XML document for a stock notification:
<notifications> <notification> <StockSymbol>AWKS</StockSymbol> <StockPrice>55.02</StockPrice> </notification> </notifications>
For a digest notification, additional notification elements are added to the schema:
<notifications> <notification> <FieldName>value</FieldName> ... </notification> <notification> <FieldName>value</FieldName> ... </notification> </notifications>
All values in the intermediate XML are strings, and there is no guarantee about the order of the notification fields within the XML.
Any field value in the intermediate XML that represents a date or a number is in the form appropriate for the locale of the notification. For example, a date for an English-United States locale is in the MM/DD/YYYY format, whereas a date for a Japanese-Japan locale is in the YYYY/MM/DD format. It is up to the application developer to do any additional translation or transformations on the notification contents.
Notification Services does not validate your XSLT file or enforce any restrictions on the transforms. Your transform must produce valid XML or plain text. However, with these two options, you can produce almost any type of document.
If you want to produce an HTML document for the notification data above, you can create the following XSL transform:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="notifications"> <html> <body> <xsl:apply-templates/> <i>Thank you for using SQL Server Notification Services.</i> </body> </html> </xsl:template> <xsl:template match="notification"> <b><xsl:value-of select="StockSymbol" /></b> is now trading at: <b> $<xsl:value-of select="StockPrice" /></b><br/> <br/><br/> </xsl:template> </xsl:stylesheet>
By default, the XSLT content formatter produces valid XML. As a result, it writes the following characters as their entity references: > (>), < (<), ' ('), " ("), and & (&).
You can configure the formatter to produce text output if you include the xsl:output element in your XSLT file. The following example is an XSL transform that returns the exact content sent to it, performing no transformations.
The following tips may help you develop your XSLT file.
The following code shows how to create an XSLT file that just returns the intermediate XML document that the distributor generates from the raw notification data. Using this transform, the content formatter actually does no transformation and simply passes through the raw notification data. Using this pass-through transform can help you verify the XML document and use your favorite development tools to create XSLT files:
Use the indent attribute of the xsl:output element to automatically indent XML:
If event data contains entity references or XML fragments, set the DisableEscaping attribute of the content formatter to true so that the content formatter does not excessively transform the data (such as converting & to &amp;). For more information about the DisableEscaping attribute, see Defining the XSLT Content Formatter.
If the event data contains reserved characters (such as &), set the DisableEscaping attribute of the content formatter to false so that the content formatter converts reserved characters into valid XML before the final transformation. For more information, see XML Reserved Characters.