XSLT <xsl:value-of> 元素

定義和用法

<xsl:value-of> 元素可提取選定節點的值。

<xsl:value-of> 元素可用于選取某個 XML 元素的值,并把它輸出。

注釋:select 屬性(必選)的值是一個 XPath 表達式。它的工作原理類似對文件系統的定位,比如用一個斜杠來選擇子目錄。

語法

<xsl:value-of
select="expression"
disable-output-escaping="yes|no"/>

屬性

屬性 描述
select expression 必需。XPath 表達式,規定了從哪個節點/屬性來提取值。
disable-output-escaping
  • yes
  • no

默認值為 "no"。

如果值為 "yes",通過實例化 <xsl:text> 元素生成的文本節點在輸出時將不進行任何轉義。

比如如果設置為 "yes",則 "<" 將不進行轉換。

如果設置為 "no",則被輸出為 "&lt;"。

實例

例子 1

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
 <html>
 <body>
   <h2>My CD Collection</h2>
   <table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

查看 XML 文件查看 XSL 文件查看結果

例子 2

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

查看 XML 文件查看 XSL 文件查看結果