建立 XSLT 檔案
若要將通知格式化,內容格式器使用每一個通知類別的一或多個 XSLT 檔。您可以定義 XSLT 檔,將每一個通知的中繼 XML 資料轉換成格式化訊息。
中繼 XML資料
對於每一個通知,散發者會將原始通知轉換成 XML 文件。對於摘要以外的所有通知,XML 文件看起來如下:
<notifications>
<notification>
<FieldName>value</FieldName>
<FieldName>value</FieldName>
...
</notification>
</notifications>
notification 元素內的元素名稱反映通知欄位名稱,如應用程式定義中所指定,且其值為通知欄位的值。通知資料是由通知資料表中的通知欄位以及計算通知欄位所組成。
例如,散發者可能為庫存通知產生下列 XML 文件:
<notifications>
<notification>
<StockSymbol>AWKS</StockSymbol>
<StockPrice>55.02</StockPrice>
</notification>
</notifications>
若為摘要通知,其他的 notification 元素會加入至結構描述中:
<notifications>
<notification>
<FieldName>value</FieldName>
...
</notification>
<notification>
<FieldName>value</FieldName>
...
</notification>
</notifications>
中繼 XML 中的所有值都是字串,但不保證 XML 內的通知欄位順序。
中繼 XML 中代表日期或數字的任何欄位值,是使用適合通知地區設定的格式。例如,English-United States 地區設定的日期是使用 MM/DD/YYYY 格式,而 Japanese-Japan 地區設定的日期則使用 YYYY/MM/DD 格式。由應用程式開發人員決定,是否要對通知內容進行任何其他翻譯或轉換。
XSL 轉換
Notification Services 不驗證 XSLT 檔案,或是在轉換時強制執行任何限制。轉換必須產生有效的 XML 或純文字。不過,利用這兩個選項,您可以產生幾乎任何類型的文件。
如果您想要為上述通知資料產生 HTML 文件,可建立下列 XSL 轉換:
<?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>
產生純文字
依預設,XSLT 內容格式器會產生有效的 XML。因此,它會寫入下列字元作為其實體參考:> (>)、< (<)、' (')、" (") 和 & (&)。
如果您在 XSLT 檔案中包含 xsl:output 元素,您可以設定格式器來產生文字輸出。下列範例是 XSL 轉換,它傳回所收到的確實內容,而不執行轉換。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="*">
Today's Notifications:
<xsl:copy-of select = "." />
</xsl:template>
</xsl:stylesheet>
XSLT 提示
下列提示可幫助您開發 XSLT 檔案。
下列程式碼顯示如何建立只傳回中繼 XML 文件的 XSLT 檔,散發者從原始通知資料產生該文件。使用這個轉換時,內容格式器實際上不執行轉換,只是傳送原始通知資料。使用這個傳遞轉換可幫助您驗證 XML 文件,並使用您最愛的開發工具來建立 XSLT 檔案:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <xsl:copy-of select = "." /> </xsl:template> </xsl:stylesheet>
使用 xsl:output 元素的 indent 屬性來自動縮排 XML:
<xsl:output method="xml" indent="yes"/>
如果事件資料包含實體參考或 XML 片段,請將內容格式器的 DisableEscaping 屬性設為 true,使內容格式器不會過度轉換資料 (例如將 & 轉換成 &amp;)。如需有關 DisableEscaping 屬性的詳細資訊,請參閱<定義 XSLT 內容格式器>。
如果事件資料包含已保留字元 (例如 &),請將內容格式器的 DisableEscaping 屬性設為 false,使內容格式器能夠在最後轉換之前,將已保留字元轉換成有效的 XML。如需詳細資訊,請參閱<XML Reserved Characters>。
請參閱
概念
XSLT 檔案位置
定義 XSLT 內容格式器
設定內容格式器
其他資源
定義通知類別
定義 Notification Services 應用程式