个性化阅读
专注于IT技术分析

XSLT xsl:choose元素

本文概述

XSLT <xsl:choose>元素用于针对具有<xsl:otherwise>和<xsl:when>元素的节点的内容指定多重条件测试。

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

参数说明

测试:它指定要测试的xml数据中的条件。


XSLT <xsl:choose>元素示例

让我们举一个例子, 通过遍历每个员工来创建一个具有其属性” id”及其子元素<firstname>, <lastname>, <nickname>和<salary>的元素表。它检查薪水, 然后打印年级详细信息。

employee.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</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> 
                  <th>Grade</th> 
               </tr> 
     
               <xsl:for-each select = "class/employee"> 
     
                  <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> 
       
                     <td> 
                        <xsl:choose> 
                           <xsl:when test = "salary > 50000"> 
                              High 
                           </xsl:when> 
         
                           <xsl:when test = "salary > 40000"> 
                              Medium 
                           </xsl:when> 
         
                           <xsl:otherwise> 
                              Low 
                           </xsl:otherwise> 
                        </xsl:choose> 
                     </td> 
                  </tr> 
               </xsl:for-each> 
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

输出

XSLT Xsl choose元素1
赞(0)
未经允许不得转载:srcmini » XSLT xsl:choose元素

评论 抢沙发

评论前必须登录!