欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

將XSLT作為HTML的樣式表的使用方法示例

WEB開發(fā)者   發(fā)布時間:2015-07-12 17:50:18   作者:佚名   我要評論
這篇文章主要介紹了將XSLT作為HTML的樣式表的使用方法示例,文中所展示的代碼在全文最后有提供下載的鏈接,需要的朋友可以參考下

 簡介  

當(dāng)聽到樣式表這個詞時,您可能會想到 CSS 樣式表。XSLT 樣式表通常用于 XML 轉(zhuǎn)換,比如在 Web 服務(wù)之間映射數(shù)據(jù)。因為 XSLT 非常適合此用途,所以創(chuàng)建了頂層元素 <stylesheet> 的 <xsl:transform> 別名,雖然這很少使用。這種 XSLT 轉(zhuǎn)換的輸入結(jié)構(gòu)與輸出結(jié)構(gòu)有很大的不同。最重要的是,命名空間的不同。

  XSLT 樣式表的輸入結(jié)構(gòu)與輸出結(jié)構(gòu)相似,但卻更簡單些。其中已經(jīng)擴(kuò)充了一些標(biāo)記,但大部分標(biāo)記只是原樣復(fù)制到輸出。輸入和輸出的命名空間是相同的 (HTML)。輸入文檔也可以包含樣式表指令(比如創(chuàng)建腳注),這些指令屬于另一個命名空間,不會傳遞到輸出中。
常用縮略語

    CSS:級聯(lián)樣式表
    XHTML:可擴(kuò)展超文本標(biāo)記語言
    XPath:XML 路徑語言
    XSLT:可擴(kuò)展樣式表語言轉(zhuǎn)換

  在本文中,我們將學(xué)習(xí)如何使用 XSLT 樣式表擴(kuò)充 XHTML 文檔。文中的示例展示了如何使用指令,如何引用其他源文檔的部分,以及如何使用鏈接在主文檔中導(dǎo)航。此外,我們還探索了頁面的解釋和編譯之間的區(qū)別。
CSS 樣式表的局限性

  XSLT 樣式表不會阻止您使用其他技術(shù),比如 JavaScript 或 CSS。CSS 適用于字體、加粗、顏色、間距等。它不適合將來自不同位置的信息組合在一起,比如腳注、模塊或生成一個目錄。這正是 XSLT 的用武之地,它補(bǔ)充而不是替代了 CSS。
XSLT 用途示例

  實際上,您可以將 XSLT 代碼集中在一個文件中。為了簡單起見,本文中的每個示例均位于一個獨(dú)立的 XSLT 文件中,除了一些必要的代碼。清單 1 給出了必需的代碼。

  清單 1. 必需的代碼(位于 samples/common.xml 中)

 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <s:stylesheet  
  2.     <span style="width: auto; height: auto; float: none;" id="24_nwp"><a style="text-decoration: none;" mpid="24" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="24_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"  
  3.     xmlns:h="http://www.w3.org/1999/xhtml"  
  4.     xmlns:a="http://sourceforge.net/projects/arbalo/"  
  5.     xmlns:s="http://www.w3.org/1999/XSL/Transform"  
  6.     exclude-result-prefixes="a h"  
  7.     version="1.0"  
  8. >  
  9.     <s:template match="h:<span style="width: auto; height: auto; float: none;" id="25_nwp"><a style="text-decoration: none;" mpid="25" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="25_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>">  
  10.         <s:copy>  
  11.             <s:apply-templates select="@*|node()"/>  
  12.             <meta  
  13.                 http-equiv="content-type"  
  14.                 content="text/html;charset=UTF-8" />  
  15.             <link  
  16.                 href="common.<span style="width: auto; height: auto; float: none;" id="26_nwp"><a style="text-decoration: none;" mpid="26" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=css&k0=css&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="26_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">css</span></a></span>"   
  17.                 rel="stylesheet"  
  18.                 type="text/css" />  
  19.         </s:copy>  
  20.     </s:template>  
  21.         
  22.     <s:template match="*">  
  23.         <s:copy>  
  24.             <s:copy-of select="@*"/>  
  25.             <s:apply-templates/>  
  26.         </s:copy>  
  27.     </s:template>  
  28. </s:stylesheet>  

  XHTML 的命名空間定義了兩次:默認(rèn)定義和 h:。默認(rèn)命名空間用于編寫輸出 XHTML 標(biāo)記,其中應(yīng)該避免使用命名空間前綴。h: 用在 XPath 表達(dá)式中。

  本文使用 XSLT 1.0 版本。目前,大部分瀏覽器都無法解釋 XSLT 2.0。但是,如果 XSLT 運(yùn)行在服務(wù)器上,那么它可能是一個實用的選擇。XSLT 2.0 還提供了:

    XPATH 2.0(if…then…else 和許多內(nèi)置的函數(shù))
    內(nèi)置和用戶編寫的 XPATH 函數(shù)
    分組

  在 清單 1 中:

    s:template match="head" 擴(kuò)充了源文檔的 head 一節(jié),添加了一個 CSS 樣式表的鏈接。即使 UTF-8 是在 XML 中的默認(rèn)編碼,一些瀏覽器也需要內(nèi)容類型才能呈現(xiàn)它。
    s:template match="*" 是默認(rèn)的詳細(xì)副本。原則上,所有內(nèi)容都會復(fù)制到目標(biāo)文檔中。如果遺漏了此模板,只會將標(biāo)記的文本內(nèi)容復(fù)制到目標(biāo)文檔。不會復(fù)制處理指令節(jié)點(diǎn)。

  本文中的所有其他示例都是導(dǎo)入 common.xsl 的獨(dú)立文件。
擴(kuò)充

  通過擴(kuò)充,添加了一個未在源文檔中顯式請求的特性。一個示例是 清單 1 中的 CSS 樣式表的鏈接。嘗試另一個示例,向每個內(nèi)部鏈接添加一個小箭頭 (^ v),指明目標(biāo)在它之前還是之后。清單 2 給出了該樣式表。

  清單 2. 樣式表(在 samples/linkUpDown.xsl 中)

 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <s:stylesheet  
  2.     <span style="width: auto; height: auto; float: none;" id="22_nwp"><a style="text-decoration: none;" mpid="22" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="22_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"  
  3.     xmlns:h="http://www.w3.org/1999/xhtml"  
  4.     xmlns:s="http://www.w3.org/1999/XSL/Transform"  
  5.     version="1.0"  
  6. >  
  7.     <s:import href="common.xsl"/>  
  8.     <s:template match="h:a[starts-with(@href,'#')]">  
  9.         <s:copy>  
  10.             <s:copy-of select="@*"/>  
  11.             <s:variable name="name" select="substring-after(@href,'#')"/>  
  12.             <s:choose>  
  13.                 <s:when test="preceding::h:a[@name=$name]">  
  14.                     <s:text>^</s:text>  
  15.                 </s:when>  
  16.                 <s:when test="following::h:a[@name=$name]">  
  17.                     <s:text>v</s:text>  
  18.                 </s:when>  
  19.             </s:choose>  
  20.             <s:apply-templates/>  
  21.         </s:copy>  
  22.     </s:template>  
  23. </s:stylesheet>  

  首先,導(dǎo)入清單 2 中的通用樣式表。模板與內(nèi)部鏈接(以 '#' 開頭)相匹配。如果鏈接指向的錨點(diǎn)位于鏈接之前,那么使用一個向上箭頭擴(kuò)充該鏈接(如果情況相反,則使用向下箭頭)。

  s:copy-of 和 s:apply-templates 可確保不會沿途丟下任何內(nèi)容。

  清單 3 給出了一個示例文檔(其中包含內(nèi)部鏈接),它經(jīng)過了清單 2 中的樣式表進(jìn)行擴(kuò)充。

  清單 3. 源文檔(在 samples/linkUpDown.xml 中)
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <?<span style="width: auto; height: auto; float: none;" id="20_nwp"><a style="text-decoration: none;" mpid="20" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="20_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>-stylesheet href="linkUpDown.xsl" type="text/xsl"?>  
  2.  <html xmlns="http://www.w3.org/1999/xhtml">  
  3.     <<span style="width: auto; height: auto; float: none;" id="21_nwp"><a style="text-decoration: none;" mpid="21" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="21_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>/>  
  4.     <body>  
  5.         <a name="a"/>  
  6.         <p>This link goes <a href="vb">downward.</a></p>  
  7.         <br/>  
  8.         <p>Reduce the size of the window to verify the link really works.</p>  
  9.         <br/>  
  10.         <a name="b"/>  
  11.         <p>This link goes <a href="^a">upward.</a>  
  12.         </p>  
  13.     </body>  
  14. </html>         

  目標(biāo)文檔看起來相同,除了清單 4 中的條目。
  清單 4. 目標(biāo)文檔(在 samples/linkUpDown.html 中)

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. … <a href="#b">v downwards.</a> …   
  2.    … <a href="#a">^ upwards.</a> …  

指令

  您可以在源文檔中添加一些指令,告訴樣式表執(zhí)行何種操作。它們屬于另一個命名空間(在本例中為前綴 a:),不會被復(fù)制到目標(biāo)文檔。

  在清單 5 中,源文檔中任何地方的指令標(biāo)記 a:ref 都會創(chuàng)建一個腳注。

  清單 5. 樣式表(在 samples/footnote.xsl 中)
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <s:stylesheet  
  2.     <span style="width: auto; height: auto; float: none;" id="19_nwp"><a style="text-decoration: none;" mpid="19" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="19_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"  
  3.     xmlns:a="http://sourceforge.net/projects/arbalo/"  
  4.     xmlns:h="http://www.w3.org/1999/xhtml"  
  5.     xmlns:s="http://www.w3.org/1999/XSL/Transform"  
  6.     version="1.0"  
  7. >  
  8.     <s:import href="common.xsl"/>  
  9.     <s:template match="h:body">  
  10.         <s:copy>  
  11.             <s:apply-templates select="@*|node()"/   
  12.             <!-- put the footnotes at the end   
  13.                 if there is no a:references directive -->  
  14.             <s:if test="not(descendant::a:references)">  
  15.                 <s:call-template name="references"/>  
  16.             </s:if>  
  17.         </s:copy>  
  18.     </s:template>  
  19.     <!-- Create a footnote -->  
  20.     <s:template match="a:ref">  
  21.         <s:variable  
  22.             name="number"  
  23.             select="count(preceding::a:ref) + 1"/>  
  24.         <a name="ref-{$number}"></a>  
  25.         <a class="footnote" href="#reference-{$number}">  
  26.             <s:value-of select="concat('v ',$number)"/>  
  27.         </a>  
  28.     </s:template>  
  29.     <!-- if a:reference is missing, assume it at the end of the body -->  
  30.     <s:template match="a:references" name="references">  
  31.         <hr/>  
  32.         <s:for-each select="//a:ref">  
  33.             <s:variable name="number" select="count(preceding::a:ref) + 1"/>  
  34.             <p>  
  35.                 <a name="reference-{$number}"></a>  
  36.                 <a class="footnote" href="#ref-{$number}">  
  37.                     <s:value-of select="concat(' ^',$number)"/>  
  38.                 </a>  
  39.                 <s:apply-templates/>  
  40.             </p>  
  41.         </s:for-each>  
  42.     </s:template>  
  43. </s:stylesheet>  

  使用源文檔中的 a:references 指令,名為 references 的模板會在模板與該指令匹配的地方分配腳注。如果缺少這樣一個指令,第一個與 body 匹配的模板會在 body 的末尾分配腳注,方法是調(diào)用名為 references 的相同模板。在兩種情況下,都會列出腳注的內(nèi)容,并生成一個由向上箭頭表示的向上鏈接。

  第二個模板(匹配 a:ref)使用向下箭頭創(chuàng)建腳注的鏈接。腳注具有編號。這里忽略了它的內(nèi)容。

  class="footnote" 屬性在 XSLT 轉(zhuǎn)換之后由一個 CSS 樣式表解析,該樣式表鏈接在 XSLT 樣式表 common.xsl 中。

  清單 6 中的源文檔使用 a:ref 指令創(chuàng)建腳注。

  清單 6. 源文檔(在 samples/footnote.xml 中)
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <?<span style="width: auto; height: auto; float: none;" id="17_nwp"><a style="text-decoration: none;" mpid="17" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="17_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>-stylesheet href="footnote.xsl" type="text/xsl"?>  
  2. <html  
  3.     xmlns="http://www.w3.org/1999/xhtml"  
  4.     xmlns:a="http://sourceforge.net/projects/arbalo/"  
  5. >  
  6.     <<span style="width: auto; height: auto; float: none;" id="18_nwp"><a style="text-decoration: none;" mpid="18" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="18_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>/>  
  7.     <body>  
  8.         <p>  
  9.             This example looks a little scientific   
  10.             <a:ref>  
  11.                 From Latin   
  12.                 <em>scientia</em>  
  13.             </a:ref>  
  14.             and academic   
  15.             <a:ref>From Greek akademia</a:ref>.   
  16.         </p>  
  17.         <p>  
  18.             Do you know why?   
  19.             <a:ref>  
  20.                 It uses   
  21.                 <em>footnotes</em>.   
  22.             </a:ref>  
  23.         </p>  
  24.         <p>Reduce size of window to verify links are generated.</p>  
  25.             
  26.         
  27.     </body>  
  28. </html>  

  目標(biāo)文檔將腳注列表包含在底部,如清單 7 所示。

  清單 7. 目標(biāo)文檔(在 samples/footnote.html 中)

 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <html  
  2.     <span style="width: auto; height: auto; float: none;" id="14_nwp"><a style="text-decoration: none;" mpid="14" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="14_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"  
  3.    xmlns:h="http://www.w3.org/1999/xhtml"  
  4.    xmlns:a="http://sourceforge.net/projects/arbalo/">  
  5.    <<span style="width: auto; height: auto; float: none;" id="15_nwp"><a style="text-decoration: none;" mpid="15" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="15_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>><link type="text/<span style="width: auto; height: auto; float: none;" id="16_nwp"><a style="text-decoration: none;" mpid="16" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=css&k0=css&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="16_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">css</span></a></span>rel="stylesheet" href="common.css"/></head>  
  6.    <body>  
  7.       <p>This example looks a little scientific   
  8.          <a name="ref-1"/><a href="#reference-1" class="footnote">v 1</a>  
  9.          and academic.   
  10.          <a name="ref-2"/><a href="#reference-2" class="footnote">v 2lt;/a>  
  11.       </p>  
  12.       <p>Do you know why?   
  13.          <a name="ref-3"/><a href="#reference-3" class="footnote">v 3</a>  
  14.       </p>  
  15.       <p>Reduce size of window to verify links are generated.</p>  
  16.       br/><br/>  
  17.    <hr/>  
  18.    <p><a name="reference-1"/><a href="#ref-1" class="footnote"> ^1</a>  
  19.       From Latin   
  20.       <em>scientia</em>  
  21.    </p>  
  22.    <p><a name="reference-2"/>  
  23.       <a href="#ref-2" class="footnote"> ^2</a>From Greek akademia</p>  
  24.    <p><a name="reference-3"/><a href="#ref-3" class="footnote"> ^3</a>  
  25.       It uses   
  26.       <em>footnotes</em>.   
  27.    </p>  
  28.    </body>  
  29. </html>  

突破源文檔的邊界

  也可引用其他源文檔的其中一些部分。a:include 指令包含一個可能屬于另一個源文檔的元素并轉(zhuǎn)換它,如清單 8 所示。

  清單 8. 樣式表(在 samples/include.xsl 中)

 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <s:stylesheet  
  2.     <span style="width: auto; height: auto; float: none;" id="12_nwp"><a style="text-decoration: none;" mpid="12" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="12_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"  
  3.     xmlns:a="http://sourceforge.net/projects/arbalo/"  
  4.     xmlns:s="http://www.w3.org/1999/XSL/Transform"  
  5.     version="1.0"  
  6. >  
  7.     <s:import href="common.xsl"/>  
  8.     <s:template  match="a:include">  
  9.         <s:choose>  
  10.             <s:when test="0!=string-length(@src)">  
  11.                 <s:apply-templates  
  12.                     select="document(@src)//*[@id=current()/@refid]"/>  
  13.             </s:when>  
  14.             <s:when test="not(@<span style="width: auto; height: auto; float: none;" id="13_nwp"><a style="text-decoration: none;" mpid="13" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=src&k0=src&kdi0=0&luki=7&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="13_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">src</span></a></span>) and //a:default[1]/@src">  
  15.                 <s:apply-templates  
  16. select="document(//a:default[1]/@src)//*[@id=current()/@refid]"/>  
  17.             </s:when>  
  18.             <s:when test="0=string-length(@src) or not(//a:default[1]/@src)">  
  19.                 <s:apply-templates  
  20.                     select="//*[@id=current()/@refid]"/>  
  21.             </s:when>  
  22.         </s:choose>  
  23.     </s:template>  
  24. </s:stylesheet>  

  源文檔中的一個 a:include 指令引用源元素的 id。包含該元素的文檔可在一個 src 屬性中命名。如果缺少該屬性,將使用 a:default 指令的 src 屬性。如果在任何地方都沒有 src 屬性,則使用同一個源文檔。因此,refid 會引用 id 來避免無限的遞歸。

  導(dǎo)入的元素可能具有一種復(fù)雜的類型,并在包含 (apply-templates)之后進(jìn)行轉(zhuǎn)換。清單 9、清單 10 和清單 11 給出了示例。

  清單 9. 源文檔(在 samples/include.xml 中)

 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <?<span style="width: auto; height: auto; float: none;" id="7_nwp"><a style="text-decoration: none;" mpid="7" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="7_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>-stylesheet href="include.xsl" type="text/xsl"?>  
  2. <html  
  3.     xmlns="http://www.w3.org/1999/xhtml"  
  4.     xmlns:a="http://sourceforge.net/projects/arbalo/">  
  5.     <<span style="width: auto; height: auto; float: none;" id="8_nwp"><a style="text-decoration: none;" mpid="8" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="8_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>>  
  6.         <a:default <span style="width: auto; height: auto; float: none;" id="9_nwp"><a style="text-decoration: none;" mpid="9" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=src&k0=src&kdi0=0&luki=7&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="9_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">src</span></a></span>="includedY.xml"/>  
  7.     </head>  
  8.     <body>  
  9.         <p>The following text is included:</p>  
  10.         <a:include refid="x" src="includedX.xml"/>  
  11.         <a:include refid="y1"/>  
  12.         <p id="i"><span style="width: auto; height: auto; float: none;" id="10_nwp"><a style="text-decoration: none;" mpid="10" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=double&k0=double&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="10_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">double</span></a></span></p>  
  13.         <a:include refid="y2"/>  
  14.         <a:include refid="i" src=""/>  
  15.     </body>  
  16. </html>  

  清單 10. 源文檔的部分(在 samples/includeY.xml 中)

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <h2 id="y2">I'm the <em>included</em> h2</h2>  
  2. <h1 id="y1">I'm the <em>included</em> h1</h1>  

  清單 11. 目標(biāo)文檔(在 samples/include.html 中)
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <body>  
  2.         <p>The following text is included:</p>  
  3.         <p id="x">I'm the <em>included</em> paragraph.</p>  
  4.         <h1 id="y1">I'm the <em>included</em> h1</h1>  
  5.         <p id="i"><span style="width: auto; height: auto; float: none;" id="6_nwp"><a style="text-decoration: none;" mpid="6" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=double&k0=double&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="6_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">double</span></a></span></p>  
  6.         <h2 id="y2">I'm the <em>included</em> h2</h2>  
  7.         <p id="i">double</p>  
  8.     </body>  
  9. </html>  

主文檔和導(dǎo)航

  如果您有一個包含多個頁面的演示,有一個主文檔包含頁面標(biāo)題及其鏈接。您可以生成完整的導(dǎo)航,從每個頁面到任何其他頁面,以及到前一個和后一個頁面。這些細(xì)節(jié)不屬于本文的介紹范圍,但 參考資料 中提供了使用主文檔的 HTML 演示的鏈接??蓪?.xml 替換為 .html 來獲得編譯后的版本。讓瀏覽器向您顯示 .xml 的整潔源代碼。您會對它生成的源代碼量感到驚奇。
解釋與編譯的對比

  解釋意味著頁面為 XML 格式(其文件擴(kuò)展名為 .xml,其內(nèi)容類型為文本/xml 或應(yīng)用程序/xml),并且處理指令所引用的 XSLT 樣式表可在瀏覽器中執(zhí)行。

  編譯意味著瀏覽器看到的是 HTML(其文件擴(kuò)展名為 .html,內(nèi)容類型為文本/html),它是在請求頁面之前從您的開發(fā)環(huán)境中或服務(wù)器上的 XML 轉(zhuǎn)換而來的。Xalan 和 Saxon 都是著名的 XSLT 處理器。

  解釋是未來的發(fā)展方向。所有現(xiàn)代瀏覽器都支持 XSLT,并且它具有一些優(yōu)點(diǎn):

    當(dāng)測試時,您會立即獲得結(jié)果。只需在您測試的每個瀏覽器中按下 F5,即可反映源頁面、CSS 和 XSLT 樣式表的更改。
    要傳遞給客戶端的信息量減少了。
    客戶端看到的是一個干凈、整潔的網(wǎng)頁,因為還未生成擴(kuò)充內(nèi)容。

  但也要注意一些缺點(diǎn):

    有一些舊瀏覽器可能不支持 XSLT。如果向一個受控的環(huán)境(內(nèi)部網(wǎng))發(fā)布頁面,就不會出現(xiàn)問題。
    一些現(xiàn)代瀏覽器禁止 XSLT 樣式表引用另一個目錄中的另一個樣式表。
    將 XSLT 域其他功能相結(jié)合(比如 SVG 或 iframe)可能在一些瀏覽器中導(dǎo)致問題。
    因為大部分瀏覽器都不支持 XSLT 2.0 或即將推出的 3.0,所以您無法使用新功能。沒有 XPath 2.0 if () then … else,也沒有用戶編寫的 XPath 功能。

  無論進(jìn)行編譯還是解釋,頁面的其他轉(zhuǎn)換 (CSS、JavaScript) 都會在 XSLT 轉(zhuǎn)換之后執(zhí)行。
結(jié)束語

  在本文中,您學(xué)習(xí)了如何使用 XSLT 樣式表來擴(kuò)充 XHTML 文檔。您可以使用本文中的示例作為起點(diǎn),構(gòu)建您自己的 XSLT 樣式表。

文章和XML示例

相關(guān)文章

最新評論