本文概述
XSLT <xsl:message>元素用于显示错误消息并帮助调试XSLT处理。它类似于JavaScript警报。该元素将消息缓冲到XSLT处理器, 后者终止处理并将消息发送给调用方应用程序以显示错误消息。
<xsl:message
terminate = "yes" | "no">
</xsl:message>
参数说明
终止:指定转换是否应在执行此指令时终止。当终止属性设置为”是”时, 元素的内容显示为系统级错误消息的一部分, 并且转换终止。当它设置为” no”时, 转换继续进行, 而忽略错误消息。默认值是”否”。
XSLT <xsl:message>元素示例
对于”是”条件
让我们以一个示例为例, 通过遍历每个员工来创建一个带有属性” id”及其子元素” <firstname>”, ” <lastname>”, ” <nickname>”和” <salary>”的<student>元素。它检查要显示的密钥为名字, 然后打印员工的详细信息, 否则显示错误消息。
employee.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
<employee id = "001">
<firstname></firstname>
<lastname>Gupta</lastname>
<nickname>Raju</nickname>
<salary>30000</salary>
</employee>
<employee id = "024">
<firstname>Sara</firstname>
<lastname>Khan</lastname>
<nickname>Zoya</nickname>
<salary>25000</salary>
</employee>
<employee id = "056">
<firstname>Peter</firstname>
<lastname>Symon</lastname>
<nickname>John</nickname>
<salary>10000</salary>
</employee>
</class>
员工.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Employee</h2>
<table border = "1">
<tr bgcolor = "pink">
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Salary</th>
</tr>
<xsl:for-each select = "class/employee">
<xsl:if test = "firstname = ''">
<xsl:message terminate = "yes">A first name field is empty.
</xsl:message>
</xsl:if>
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "salary"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出
对于”无”条件
员工.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Employee</h2>
<table border = "1">
<tr bgcolor = "pink">
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Salary</th>
</tr>
<xsl:for-each select = "class/employee">
<xsl:if test = "firstname = ''">
<xsl:message terminate = "no">A first name field is empty.
</xsl:message>
</xsl:if>
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "salary"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出