XSL應用實例

<script language="javascript" type="text/javascript"> document.title="XSL應用實例 - "+document.title </script>

1.   XML與配對的XSL文件

a)        1.xml

<?xml version="1.0" encoding="utf-8"?>

<?xml-stylesheet type="text/xsl" href="1.xsl"?>

<forum>

     <board id="1" name="Java Programming">

         <message id="1"/>

     </board>

     <board id="2" name="C++ Programming">

         <message id="2"/>

     </board>

     <board id="3" name="XML Programming">

         <message id="3"/>

     </board>

</forum>

 

b)        1.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="html"/>

     <xsl:template match="/">

         <html>

              <head>

                   <title>Discussion Forum</title>

              </head>

              <body>

                   <ul>

                       <xsl:apply-templates select="forum/board"/>

                   </ul>

              </body>

         </html>

     </xsl:template>

     <xsl:template match="board">

         <li>

              <xsl:value-of select="@name"/>

              <ul>

                   <xsl:apply-templates select="message"/>

              </ul>

         </li>

     </xsl:template>

     <xsl:template match="message">

         <li>

              Message <xsl:value-of select="@id"/>

         </li>

     </xsl:template>

</xsl:stylesheet>

 

2.   <xsl:apply-templates><xsl:template><xsl:call-template>

a)        <xsl:apply-templates>

                      i.              使用:

<xsl:apply-templates select="…" mode="…"/>

                   ii.              說明:<xsl:apply-templates>的作用就是從當前的路徑開始,根據select的內容生成一個候選的節點列表,再對列表中的每一節點,根據mode屬性的設定,查找某個匹配的<xsl:template>標記,並進行替換。

 

b)        <xsl:template>

                      i.              使用:

<xsl:template match="…" name="…" priority="…" mode="…">

</xsl:template>

                   ii.              說明:<xsl-template>包含name屬性時,則該模板將只能被<xsl:call-template>標記調用,而不會與候選的節點列表進行匹配。當有多個<xsl:template>與指定的節點匹配成功時,則最後遇上(或處於文件中更後位置)的<xsl:template>將被使用。若使用priority屬性,則可以改變這種缺省的順序,priority的值越大,則該<xsl:template>標記更會被優先使用。Mode參數與<xsl:apply-templates>中的mode值對應。

 

c)        <xsl:call-template>

                      i.              使用:<xsl:call-template name="…"/>

                   ii.              說明:name屬性與<xsl-template>中的name屬性相對應,用於指明將調用哪個<xsl:template>

 

3.   流控制

a)        <xsl:if>

                      i.              使用:

<xsl:if test="…">

    

</xsl:if>

                   ii.              說明:當test內的表達式爲真時,則執行標記內的內容。在XSLT中只定義了<xsl:if>標記,而沒有elseelse if標記,因此,若需要表示多種選擇時,可採用多個<xsl:if>標記,或使用<xsl:choose>標記。

 

b)        <xsl:choose>

                      i.              使用:

<xsl:choose>

     <xsl:when test="…">…</xsl:when>

     <xsl:when test="…">…</xsl:when>

     <xsl:otherwise>…</xsl:otherwise>

</xsl:choose>

                   ii.              說明:<xsl:choose>標記的作用類試於switch操作,但比switch具有更多的判斷類型。

 

 

c)        <xsl:for-each>

                      i.              使用:

<xsl:for-each select="…">

</xsl:for-each>

                   ii.              說明:該標記的作用與for循環操作類試。也可以使用配對的<xsl:apply-templates><xsl:template>來等效替換該<xsl:for-each>操作。例以1.xml爲例

1.        <xsl:apply-templates><xsl:template>

<xsl:apply-tmeplates select="message"/>

<xsl:template metch="message">

     Message <xsl:value-of select="@id"/>

</xsl:template>

2.        <xsl:for-each>

<xsl:for-each select="message">

     Message <xsl:value-of select="@id"/>

</xsl:for-each>

 

4.   參數和變量

a)        <xsl:variable>

                      i.              使用:

<xsl:variable name="var1">index.html</xsl:variable>

<xsl:variable name="var2" select="index.html"/>

                   ii.              說明:<xsl:variable>應該放於<xsl:template>內。若要調用該變量時,則有兩種情況:若是在以xsl爲前綴的標記內使用則使用“$變量名”的格式;而在其他情況則需要使用

{$變量名}”的格式。

 

b)        <xsl:param>

                      i.              使用:

<xsl:param name="par1" select="…"/>

                   ii.              說明:在<xsl:template>中嵌入<xsl:param>標記可獲取由<xsl:apply-templates><xsl:call-template>中傳遞過來的參數值,select屬性用於爲該參數提供一個缺省值。

 

c)        <xsl:with-param>

                      i.              使用:

<xsl:with-param name="par1" select="…"/>

                   ii.              說明:在<xsl:apply-templates><xsl:call-template>可嵌入<xsl:with-param>標記向目標的<xsl:template>傳入一個參數。該參數值由select屬性指定。

 

 

 

 

5.   輸出

a)        <xsl:output>

                      i.              使用:

<xsl:output method="…" indent="…"/>

                   ii.              說明:該標記一般位於文檔的開頭部分用於指明目標文檔的屬性。method的值一般爲“html”,“xml”或“text”。其他的屬性定義可參考《Java & XSLT》和XSLT的規範。

 

b)        <xsl:value-of>

                      i.              使用:

<xsl:value-of select="…" disalbe-output-esacping="…"/>

                   ii.              說明:用於輸出select所指定的值。disable-output-escaping用於指定某些符號是否可以不用轉換,例如當<xsl:value-of>的輸出值中包含“>”,則當該符號輸出時將被轉換爲“&gt;”,而若disable-output-escaping被指爲true的話,則“>”將輸出爲“>”。特別的,當<xsl:output>method屬性指定爲text時,該屬性值將被忽略,“>”符號將輸出爲“>”。

 

c)        <xsl:text>

                      i.              使用:

<xsl:text disable-output-escaping="…">…<xsl:text>

                   ii.              說明:輸出標記之間的內容,disable-output-escaping屬性的含義與<xsl:value-of>同。

 

d)        <xsl:strip-space><xsl:preserve-space>

                      i.              使用:

<xsl:strip-space elements="…"/>

<xsl:preserve-space element="…"/>

                   ii.              說明:該兩個標記都是<xsl:stylesheet>的直接子標記,用於指明是否保留elements屬性指定的元素中的空格。

 

e)        <xsl:processing-instruction>

                      i.              使用:

<xsl:processing-instruction name="…">…<xsl:processing-instruction>

                   ii.              說明:用於在目標文檔中生成一個由name屬性指定的processing-instruction

 

f)        <xsl:message>

                      i.              使用:

<xsl:message terminate="…">…<xsl:message>

                   ii.              說明:向目標文檔輸出標記間的內容,terminate屬性用於指明XSLT處理器在輸出該內容後,是否需要中止,缺省的屬性值爲“no”

 

 

 

 

 

 

g)        <xsl:copy><xsl:copy-of>

                      i.              使用:

<xsl:copy />

<xsl:copy-of select="…"/>

                   ii.              說明:<xsl:copy>用於複製當前節點到目標文件,但不包括當前節點的屬性和子節點。<xsl:copy-of>用於遞歸的複製由select屬性所指定的節點,包括屬性,子節點等。

 

6.   節點生成

a)        <xsl:attribute>

                      i.              使用:

<xsl:attribute name="…" namespace="…">…<xsl:attribute>

                   ii.              說明:定義一個由name指定的屬性值。

 

b)        <xsl:attribute-set>

                      i.              使用:

<xsl:attribute-set name="…" use-attribute-set="…">

     <xsl:attribute name="…">…<xsl:attribute>

    

</xsl:attribute-set>

                   ii.              說明:定義一個由name命名的屬性集合。通過use-attribute-set可以引入另一個屬性集。

 

c)        <xsl:element>

                      i.              使用:

<xsl:element name="…" namespace="…" use-attribute-set="…">…<xsl:element>

                   ii.              說明:產生一個由name指定的元素,通過use-attribute-set可以指定一個屬性集。

 

7.   文檔導入

a)        <xsl:include>

                      i.              使用:<xsl:include href="…"/>

                   ii.              說明:導入由href指定的外來xsl文件,被導入文檔中的template的優先級與當前文檔一致,且<xsl:include>標記能出現於當前文檔中的任何位置。

 

b)        <xsl:import>

                      i.              使用:<xsl:import href="…"/>

                   ii.              說明:導入由href指定的外來xsl文件,被導入文檔中的template的優先級低於當前文檔中的同名template,另<xsl:import>標記應置於文檔開始部分。

 

8.   其他

a)        <xsl:sort>

                      i.              使用:

<xsl:sort select="…" order="…" case-order="…" data-type="…"/>

                   ii.              說明:對select屬性指定的節點列表進行排列。order的候選值爲“ascending”和“descending”。case-order的候選值爲“uppper-first”和“lower-first”。data-type的候選值爲“text”和“number”。text表明按字符順序排列,number表明按數值大小進行排列。<xsl:sort>標記一般處於<xsl:apply-templates><xsl:for-each>中。

 

9.   XSLT的缺省規則

a)        遞歸處理各個節點

<xsl:template match="*|/">

<xsl:apply-templates />

         </xsl:template>

 

b)        遞歸輸出所有文本內容和屬性值

<xsl:template match="text()|@*">

     <xsl:value-of select="."/>

</xsl:template>

 

c)        忽略所有processing-instructioncomment

<xsl:template match="processing-instruction()|comment()"/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章