淺談XML和XSLT結合使網站設計渾然一體_Xml教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:在XPath查詢中指定軸
      以下示例顯示如何在 XPath 查詢中指定軸。這些示例中的 XPath 查詢都在 SampleSchema1.xml 中所包含的映射架構上指定。有關此示例架構的信息,請參見示例 XPath 查詢。 示例 A. 檢索上下文

      XML和XSLT的轉換使Web設計受益無窮。借助XML和 XSLT轉換,你可以實現將動態用語(dynamic verbiage)和網站內容存儲在數據庫中。你可以在XML中傳輸數據庫,然后再通過XSLT轉換將其轉變為HTML腳本。

      在網絡發展初期,凝聚性(cohesiveness)是由服務器端實現的,但要牽涉到大量的人工文件管理工作。幸運的是,隨著網絡的日益成熟,網絡開發工具也日臻完善。例如,在.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="http://www.wf0088.com/Submit">
      </form>
      </body>
      </html>  
       

      以上代碼段完成了主要功能,但還需用XML和XSLT來對其加以美化。

      在XML中,代碼有開頭和結尾標簽,而在HTML中沒有。INPUT 和BR標簽是個特例,它們不需結尾標簽。然而,在結尾標簽標記“>”前加一個正斜杠,可確保HTML符合XML規范。如果在編寫HTML腳本時注意遵從這些規范,你就能夠將XML/HTML(aka 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="http://www.wf0088.com/Submit"
      transform="bigButton"/>
      </form> 運行下列代碼,完成XSLT轉換:
      <?xml version="1.0"?>
      <xsl:stylesheet
      xmlns:xsl="http://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標簽的轉換屬性驅動轉換操作。我曾考慮用一個FORM窗體作為定義轉換操作所需的用戶控件的單元,因為所有用于用戶輸入的控件都應在一個FORM中。本例中,輸出為一個文本INPUT,文本顏色為藍色;一個高20像素、寬100像素的按鈕,字體為18點加粗。我們可以通過修改轉換屬性來改變文本框中的文本顏色。

      有多種方法可將靜態內容添加到網頁中本例中只采用最簡單的方式,即在樣式表中增加header和footer。

      現在,要創建一個新窗體用于用戶輸入時,要做的只是創建一個一般窗體。一旦一般窗體通過測試,就可以將這些窗體添加到轉換中生成主題的HTML輸出。你只要記住輸入控件類型,并注意把它添加為轉換屬性即可。

      分享:XML讀取數據到內存
      從XML中讀取數據到內存的實例: public clsSimuResultByOneGoods GetOneGoodsSimulationXML(string PathAndFileName) {

      來源:模板無憂//所屬分類:Xml教程/更新時間:2009-05-19
      相關Xml教程