XSL概述(二)

來源:互聯網
上載者:User
文章目錄
  • 首先,來看一下基本的Xpath運算式。

Xpath查詢是XSLT的重中之重,唯一的查詢方式。

許多的xslt結點都採用Xpath查詢運算式,包括xsl:template,xsl:value-of,等等。

首先,來看一下基本的Xpath運算式。

2.xml是一個範例xml,2.xsl中包含了很多Xpath運算式,主要是通過父子關係及萬用字元來查詢。

"." 當前結點:<xsl:value-of select="./@name"/><br/>
"." 可以省略:<xsl:value-of select="@name"/>
<hr/>
"./部門"表示該結點下所有tagname為部門的兒子結點<br/>
".//",這代表該結點的所有tagname為員工的子孫結點<br/>
<xsl:for-each select="./部門">
     部門名稱:<xsl:value-of select="@name"/>,部門員工數量:<xsl:value-of select="count(.//員工)"/><br/>
</xsl:for-each>
二級部門合計:<xsl:value-of select="count(./部門)"/>
各級部門合計:<xsl:value-of select="count(.//部門)"/>
<hr/>
"*" 表示通配所有結點<br/>
<xsl:for-each select=".//*">
     名稱:<xsl:value-of select="./@name"/>,<br/>
</xsl:for-each>
<br/>
"./*/員工" 表示當前結點下的tagname為員工的孫子結點<br/>
二級部門的直屬員工<br/>
<xsl:for-each select="./*/員工">
     暱稱:<xsl:value-of select="current()/@name"/>,<br/>
</xsl:for-each>
"./*/員工" 表示當前結點下的tagname為員工的孫子結點<br/>
三級部門的直屬員工<br/>
<xsl:for-each select="./*/*/員工">
     暱稱:<xsl:value-of select="./@name"/>,<br/>
</xsl:for-each>

第二,來歸納一下Xpath的基本運算子和邏輯運算子

/ 兒子結點或屬性
// 兒孫結點
. 當前結點    
* 通配所有結點 例如 ./*表示當前結點的所有兒子結點
@ 屬性的首碼   例如 ./@name 表示當前結點的name屬性,而./name 表示當前結點的name結點,
@* 屬性萬用字元
: 名稱空間
( ) 分組  例如 (./child[1] and ./child[2]) or (./child[3] and ./child[4])
[ ] 篩選條件運算式 例如 ./child[position()=1]
[ ] 下標  例如./child[1]
+  
-  
div  
*  
mod  
Operator Description
and Logical-and
or Logical-or
not() Negation
= Equality
!= Not equal
&lt; * Less than
&lt;= * Less than or equal
&gt; * Greater than
&lt;= * Greater than or equal
| Set operation; returns the union of two sets of nodes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.