初探XSL,XSLT,XPath以及其他

來源:互聯網
上載者:User

      要說接觸這些W3C的語言或是標準,肯定都是從HTML語言開始的.我也不例外,而且因為HTML的簡單易懂(當然說的是10年前,現在已經擴充的非常難懂了),因此對於這些標記語言比如XML,就沒有太在意.最近項目中就有個模組是吧普通的網頁抓取過來,然後用XPath去分析HTML的指令碼,然後把需要的結果使用XML進行儲存,這樣就把資料和顯示分開了,最後再根據用戶端的不同,使用不同的XSLT進行轉換,產生和裝置端對應的HTML指令碼供瀏覽.
     可以看出來裡邊涉及了很多的標記語言,因此今天就打算粗略介紹一些這些知識.就我個人觀點,除非馬上就要用,否則這種淺嘗輒止的瞭解就足夠了.我是在W3Schools裡系統的瞭解了一番,簡要介紹在這裡.
    
1.    XSL 
    XSL應該是和XML相對應的,一個是描述資料的,一個是描述怎樣表示和處理資料的.XSL分為三個部分.第一,XSLT,就是怎樣表現資料的語言. 第二,XPath,怎樣搜尋資料的語言.第三,XSL-FO,格式化資料的語言(好像沒聽說過). 一個比較特殊的執行個體就是CSS和HTML,HTML應該說是一種特殊的XML,只是有了預定義的標籤.而CSS,是表示如何顯示HTML的語言,而HTML的臃腫定義已經包含了很多的展示資料的標籤比如<B></B><I></I>等.

2. XSLT
    XSLT使用XPath進行XML資料的檢索.根節點是<xsl:stylesheet> 或者是 <xsl:transform>.  
    <xsl:template match="/"> 元素指定了在什麼位置應用模板中定義的轉換方法進行轉換.
    <xsl:value-of select="..."/>元素,其中value-of表示把XML中的資料顯示在這裡,後邊的select是XPath語句,表示只顯示XPath篩選出來的資料.
    <xsl:for-each select="aa[bb='cc']">這個元素是迴圈檢索顯示的符合標準的資料,XPath指在aa節點下,所有bb屬性等於cc的資料.
    <xsl:sort select="aa"/> 指定了最終資料的現實按照aa節點的值進行排序.
    <xsl:if test="price &gt; 10">條件判斷元素,只顯示滿足條件的元素,注意邏輯判斷符號<,>,都要用&gt;,&lt;來代替.
    <xsl:choose><xsl:when test="price &gt; 10"></xsl:when><xsl:otherwise></xsl:otherwise></xsl:choose> 和程式設計語言中的Case...When語句一樣,對資料按條件進行歸類.
    <xsl:apply-templates>指對當前元素以及當前元素的子項目應用符合要求的Template.
    在用戶端,可以使用JavaScript應用XSLT吧XML翻譯成XHTML進行顯示.這樣會依賴用戶端瀏覽器的規範.
    在服務端,可以用ASP,.Net等語言使用平台的XML功能支援進行轉換.
3.XPath
    一系列的其他語言XQuery,XLink,XPointer和XSLT都依賴於XPath進行資料檢索.XPath功能強大內建了上百個function.
Nodes 包括Document Node, Element Node 和Attribute Node.
一些資料檢索文法如下:

Expression Description
nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

檢索語句中可以包含一個用[]指定的邏輯判斷語句,表示只擷取合格資料.

萬用字元

Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind

使用 | 把兩個或者多個路徑條件組合起來,結果是單獨檢索獲得的結果的和.

XPath Axis,表示節點之間的關聯準則.

AxisName Result
ancestor Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute Selects all attributes of the current node
child Selects all children of the current node
descendant Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following Selects everything in the document after the closing tag of the current node
following-sibling Selects all siblings after the current node
namespace Selects all namespace nodes of the current node
parent Selects the parent of the current node
preceding Selects everything in the document that is before the start tag of the current node
preceding-sibling Selects all siblings before the current node
self Selects the current node

XPath 邏輯判斷

Operator Description Example Return value
| Computes two node-sets //book | //cd Returns a node-set with all book and cd elements
+ Addition 6 + 4 10
- Subtraction 6 - 4 2
* Multiplication

6 * 4

24
div Division 8 div 4 2
= Equal price=9.80 true if price is 9.80
false if price is 9.90
!= Not equal price!=9.80 true if price is 9.90
false if price is 9.80
< Less than price<9.80 true if price is 9.00
false if price is 9.80
<= Less than or equal to price<=9.80 true if price is 9.00
false if price is 9.90
> Greater than price>9.80 true if price is 9.90
false if price is 9.80
>= Greater than or equal to price>=9.80 true if price is 9.90
false if price is 9.70
or or price=9.80 or price=9.70 true if price is 9.80
false if price is 9.50
and and price>9.00 and price<9.90 true if price is 9.80
false if price is 8.50
mod Modulus (division remainder) 5 mod 2 1

4. XQuery
    XQuery應用XPath對XML資料進行檢索,結果不是節點集合,而是一個子XML資料.類似於使用SQL語句檢索資料庫
5. XLink和XPointer
    XLink定義了在XML中加入超連結的基本方法,而XPointer定義了更有針對性的定義超連結到指定XML資料.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.