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 |
1066

被折叠的 条评论
为什么被折叠?



