XSLT <xsl:value-of> 元素

定义和用法

<xsl:value-of> 元素可提取选定节点的值。

<xsl:value-of> 元素可用于选取某个 XML 元素的值,并把它输出。

注释:select 属性(必选)的值是一个 XPath 表达式。它的工作原理类似对文件系统的定位,比如用一个斜杠来选择子目录。

语法

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

属性

属性值描述
selectexpression必需。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 文件,查看结果。