XML教程
XQuery教程
XSLT教程
XPath教程

XSLT <xsl: message>

XSLT <xsl: message>元素

XSLT <xsl: message>元素用于显示错误消息并帮助调试XSLT处理。它类似于JavaScript警报。该元素将消息缓冲到XSLT处理器,该处理器终止处理并将消息发送给调用方应用程序以显示错误消息。
<xsl:message 
   terminate = "yes" | "no">
</xsl:message>

参数说明

终止: 它指定是否应在执行此指令后终止转换。当终止属性设置为"是"时,元素的内容显示为系统级错误消息的一部分,并且转换终止。如果将其设置为" no",则转换将继续进行,而忽略错误消息。默认值为"否"。

XSLT <xsl: message>元素示例

对于"是"条件

让我们以创建一个通过遍历每个员工,其属性为" id"的子元素 及其子元素<firstname>,<lastname>,<nickname>和<salary>。它将检查密钥作为名字出现,然后打印员工的详细信息,否则显示错误消息。
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>
Employee.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>
输出:
XSLT Xsl消息元素1

针对"否"条件

Employee.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>
输出:
XSLT Xsl消息元素1
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4