文章目錄
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 |
| < * |
Less than |
| <= * |
Less than or equal |
| > * |
Greater than |
| <= * |
Greater than or equal |
| | |
Set operation; returns the union of two sets of nodes |