XPath通配符

XPath在要與XPath表達式一起使用的節點上定義以下通配符。

序號

通配符

描述

1

*

用於匹配任何節點。

2

.

用於匹配上下文中的當前節點。

3

@*

用於匹配任何屬性。

4

node()

用於匹配任何類型的節點。

示例

此示例通過迭代每個學生創建一個包含其詳細信息的<student>元素。

文件:students.xml -

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Su</lastname>
      <nickname>MaXX</nickname>
      <marks>88</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Lee</lastname>
      <nickname>Vicky</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Wong</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

文件:students.xsl -

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h2>學生信息列表如下 - </h2>
            <xsl:apply-templates select = "class/*" />
         </body>
      </html>
   </xsl:template>

   <xsl:template match = "class/*">
      <xsl:apply-templates select = "@rollno" />
      <xsl:apply-templates select = "firstname" />
      <xsl:apply-templates select = "lastname" />
      <xsl:apply-templates select = "nickname" />
      <xsl:apply-templates select = "marks" />
      <br />
   </xsl:template>

   <xsl:template match = "@rollno">
      <span style = "font-size = 22px;">
         <xsl:value-of select = "." />
      </span>
      <br />
   </xsl:template>

   <xsl:template match = "firstname">
      名字:<span style = "color:blue;">
         <xsl:value-of select = "." />
      </span>
      <br />
   </xsl:template>

   <xsl:template match = "lastname">
      姓氏:<span style = "color:green;">
         <xsl:value-of select = "." />
      </span>
      <br />
   </xsl:template>

   <xsl:template match = "nickname">
      暱稱:<span style = "color:red;">
         <xsl:value-of select = "." />
      </span>
      <br />
   </xsl:template>

   <xsl:template match = "marks">
      分數:<span style = "color:gray;">
         <xsl:value-of select = "." />
      </span>
      <br />
   </xsl:template>

</xsl:stylesheet>

使用瀏覽器打開上面文件:student.xsl,顯示效果如下所示 -

XPath通配符