如果你還沒認真考慮過用一種緊密結合的單一方式來做網頁設計,這篇值得看看。
網路發展初期,「凝聚性」是靠伺服器端硬撐的,代價是大量人工的文件維護工作。好消息是隨著網路成熟,開發工具也越來越完善——例如在 .NET 框架下,你可以建立各種 Web 控制項來統一設計。
XML 與 XSLT 的轉換則是另一條路,而且效果驚人。有了這套組合,你可以把動態文案與網站內容都存進資料庫,以 XML 形式取出,再透過 XSLT 轉換成 HTML。本文用一個實際網站為例,說明 XML 與 XSLT 如何讓網站設計渾然一體。
設計使用者與資料的互動功能時,我在意的是三件事:資料的完整性、使用者介面的功能性、商業規則的正確實作。我最不在意的是按鈕要什麼顏色——偏偏這類細節最容易讓工程師忍不住分心。
所以設計一個全新頁面時,我只投入最低限度的力氣在介面上:一個文字框、一個送出鈕,如此而已。本例的 HTML 就是兩個 INPUT 標籤:
<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 時養成遵守這些規範的習慣,你就能把 XML/HTML(也就是 XHTML)直接轉換成漂亮的 HTML 頁面。看看修好之後的樣子:
<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 屬性——這是自訂屬性,瀏覽器不認得也不影響,但 XSLT 會拿它當作轉換的判斷依據。
<?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>
樣式表中,我用 HTML 標籤的 transform 屬性來驅動整個轉換流程。之所以以 FORM 窗體為單位來定義使用者控制項,是因為所有供輸入的控件理應都在同一個 FORM 裡。
本例的最終輸出是:一個文字 INPUT(文字為藍色),加上一個高 30 像素、寬 100 像素、18 點粗體的按鈕。想換文字框的顏色?改樣式表裡的 transform 對應規則就好,不用碰原本的表單。
靜態內容的加入方式有很多種,這裡為了示範,我用了最簡單的一招:直接在樣式表裡放 header 與 footer。
現在當我要做一個新的使用者輸入窗體時,流程變成:
唯一要記住的是:新增的控件型別別忘了在樣式表裡補一條對應的 transform 規則。
達到目的的方法有很多種,但這個例子想傳達的是一個觀念——把 HTML 輸出標準化。當呈現層被收斂到樣式表裡,頁面的外觀就不再散落在數十個檔案中,改版也從「逐頁修改」變成「改一處」。