Latest web development tutorials

XSLT <xsl:value-of> 元素

<xsl:value-of> 元素用於提取某個選定節點的值。


<xsl:value-of> 元素

<xsl:value-of> 元素用於提取某個XML 元素的值,並把值添加到轉換的輸出流中:

實例

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

嘗試一下»

實例解釋

注意:在上面的實例中, select屬性的值是一個XPath表達式。這個XPath 表達式的工作方式類似於定位某個文件系統,在其中正斜杠(/)可選擇子目錄。

上面實例的結果有一點小缺陷,僅有一行數據從XML 文檔被複製到輸出。 在下一章中,您將學習到如何使用<xsl:for-each>元素來循環遍歷XML元素,並顯示所有的記錄。