<xsl:template> 元素

<xsl:template> 元素用于构建模板。
match 属性用于关联 元素和模板。match 属性也可用来为整个 文档定义模板。match 属性的值是 XPath 表达式(举例,match=\"/\" 定义整个文档)。

注意:由于 XSL 样式表本身也是一个 文档,因此它总是由 声明起始:<? version=\"1.0\" encoding=\"ISO-8859-1\"?>.
下一个元素,<xsl:stylesheet>, ,定义此文档是一个 XSLT 样式表文档(连同版本号和 XSLT 命名空间属性)。

<xsl:value-of> 元素

<xsl:value-of> 元素用于提取某个 元素的值,并把值添加到转换的输出流中
select 属性的值是一个 XPath 表达式

<xsl:for-each> 元素

XSL <xsl:for-each> 元素可用于选取指定的节点集中的每个 元素
通过在 <xsl:for-each> 元素中添加一个选择属性的判别式,我们也可以过滤从 文件输出的结果。

<xsl:for-each select=\"catalog/cd[artist=\'Bob Dylan\']\">

如需对输出结果进行排序,只要简单地在 XSL 文件中的 <xsl:for-each> 元素内部添加一个 <xsl:sort> 元素

<xsl:sort select=\"artist\"/>

<xsl:if> 元素

<xsl:if test=\" \">
...如果条件成立则输出...
</xsl:if> 

<xsl:choose> 元素

<xsl:choose>
<xsl:when test=\" \">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose> 

PHP服务器把 转换为 XHTML

<?php
// 载入   文件
$  = new DOMDocument;
$ ->load(\'cdcatalog. \');

// 载入 XSL 文件
$xsl = new DOMDocument;
$xsl->load(\'cdcatalog.xsl\');

// 设置转换
$proc = new XSLTProcessor;

// 添加 xsl 规则
$proc->importStyleSheet($xsl);

echo $proc->transformTo ($ );
?>

实例

test.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<? -stylesheet type=\"text/xsl\" href=\"decorate.xsl\"?>
<messageList>
	<message>
		< >go</ >
		<author>Tom</author>
		<info>time to go</info>
	</message>
	<message>
		< >back</ >
		<author>Jim</author>
		<info>time to back</info>
	</message>
	<message>
		< >run</ >
		<author>Jack</author>
		<info>time to run</info>
	</message>
</messageList>

decorate.xsl

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<xsl:stylesheet version=\"2.0\"  ns:xsl=\"http://www.w3.org/1999/XSL/Transform\"  ns:fo=\"http://www.w3.org/1999/XSL/Format\"  ns:xs=\"http://www.w3.org/2001/ Schema\"  ns:fn=\"http://www.w3.org/2005/xpath-functions\">
<xsl:template match=\"/\">
	<html>
		<head>
		</head>
		<body>
			<h2>Message</h2>
			<table>
				<tbody>
					<tr>
						<th> </th>
						<th>author</th>
						<th>info</th>
					</tr>
					<xsl:for-each select=\"messageList/message\">
						<tr>
							<td><xsl:value-of select=\" \"/></td>
							<td><xsl:value-of select=\"author\"/></td>
							<td><xsl:value-of select=\"info\"/></td>
						</tr>
						<!--注意:在遍历里面的value-of的select属性路径只需要写相对for-each的路径即可!-->
					</xsl:for-each>
				</tbody>
			</table>
		</body>
	</html>
</xsl:template>
</xsl:stylesheet>

效果:
\"在这里插入图片描述\"

收藏 打印