XSLT <xsl:value-of> 元素

定義和用法
<xsl:value-of> 元素可提取選定節點的值。
<xsl:value-of> 元素可用於選取某個 XML 元素的值,並把它輸出。
語法
<xsl:value-of
select="expression"
disable-output-escaping="yes|no" />
屬性
屬性 | 值 | 描述 |
---|---|---|
select | expression | 必需。一個 XPath 運算式,規定了從哪個節點/屬性來提取值。它的工作原理與通過正斜杠(/)選擇子目錄的導航檔系統類似。 |
disable-output-escaping | yes no |
可選。如果值為 "yes",通過實例化 <xsl:text> 元素生成的文本節點在輸出時將不進行任何轉義。比如 "<" 將輸出為 "<"。如果值為 "no",則 "<" 將輸出為 "<"。默認是 "no"。 |
實例
下麵的實例獲取第一個 title 和 artist 元素的值,並放置在表格中:
實例 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>
<h1>Music Collection:</h1>
<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>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Music Collection:</h1>
<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>
下麵的實例迴圈遍曆每個 cd 元素,並創建帶有每個 cd 元素的 title 和 artist 的值的表格行:
實例 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>
<h1>Music Collection:</h1>
<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>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Music Collection:</h1>
<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>
