XML 與 XSLT 的組合能為 Web 設計帶來極大效益。透過這套技術,你可以把動態文案與網站內容統一存進資料庫,以 XML 承載資料輸出,再用 XSLT 轉換成 HTML 頁面。
網路發展初期,「一致性」多半靠服務端程式硬撐,伴隨而來的是大量人工文件維護工作。所幸隨著網路日趨成熟,開發工具也越來越完善。例如在 .Net 框架下,你可以建立各類 Web 控制項來統一設計風格。
本文將透過一個實站案例,說明在設計使用者與資料互動的功能時,如何兼顧資料完整性、介面功能性與商業規則,並看看 XML 與 XSLT 是怎麼把整個網站設計串成一體的。
<html>
<head>
</head>
<body>
<form method="POST" name="thisForm" id="thisForm" action="somepage.php">
<input type="text" name="txtText" id="txtText" size="25"><br>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit">
</form>
</body>
</html>
功能上沒問題,但還需要 XML 與 XSLT 來替它加上一致的樣式與結構。
在 XML 中,每個標籤都有明確的開頭與結尾;HTML 則不然,INPUT 與 BR 是特例,不需要結束標籤。不過只要在結尾的「>」前補上一個斜線,就能讓 HTML 符合 XML 規範。養成這個習慣寫 HTML(也就是 XHTML),你就能把它順利轉換成完整的頁面。
<form method="POST" name="thisForm" id="thisForm" action="somepage.php">
<input type="text" name="txtText" id="txtText" size="25" transform="blueText"/>
<br/>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" transform="bigButton"/>
</form>
注意這裡多出的 transform 屬性——它就是驅動後續轉換的關鍵。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td align="center">This is the defined header</td></tr>
<tr><td><xsl:apply-templates select="//form"/></td></tr>
<tr><td align="center">This is the defined footer</td></tr>
</table>
</xsl:template>
<xsl:template match="form">
<xsl:element name="form">
<xsl:attribute name="method"><xsl:value-of select="@method"/></xsl:attribute>
<xsl:attribute name="action"><xsl:value-of select="@action"/></xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:apply-templates select="*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="@transform='blueText'">
<xsl:element name="input">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="type">text</xsl:attribute>
<xsl:attribute name="style">color:blue</xsl:attribute>
<xsl:if test="@value"><xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute></xsl:if>
</xsl:element>
</xsl:when>
<xsl:when test="@transform='redText'">
<xsl:element name="input">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="type">text</xsl:attribute>
<xsl:attribute name="style">color:red</xsl:attribute>
<xsl:if test="@value"><xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute></xsl:if>
</xsl:element>
</xsl:when>
<xsl:when test="@transform='bigButton'">
<xsl:element name="input">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="style">height:30px;width:100px;font-size:18pt;font-weight:700;</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
這段樣式表並不負責建立命名空間、定義 XML 標籤或驗證 DTD/schema;它的任務是把一份可行的 HTML 片段轉化成完整的新頁面,讓你不必在每個頁面重複操心設計細節。
樣式表中,我們用 HTML 標籤上的 transform 屬性來驅動轉換行為。之所以以 FORM 窗體作為單元,是因為所有供使用者輸入的控制項都應該收在同一個 form 裡。
在本例中,輸出結果是一個文字 INPUT,文字顏色為藍色;還有一顆高 30 像素、寬 100 像素、18pt 粗體的按鈕。想改文字顏色,只要改 transform 屬性值即可——例如把它從 blueText 換成 redText。
至於靜態內容(頁首與頁尾),加入方式有很多種,這裡採用最簡單的一種:直接在樣式表中寫死 header 與 footer。
往後要新增一個使用者輸入窗體時,流程很單純:先寫一個普通窗體,測試通過後把它加進轉換流程,就能產出套用相同主題的 HTML。你只要記住控制項的類型,並在標籤上加上對應的 transform 屬性即可。
這正是 XML 與 XSLT 的價值所在:資料與樣式徹底分離,站點的一致性由樣式表集中管理,而不是散落在數十個頁面裡。