XSLT <value-of>標籤

<xsl:value-of>標記將所選節點的值按照XPath表達式設置爲文本。

聲明

以下是<xsl:value-of>元素的語法聲明。

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

屬性

  • Select - 要在當前上下文中計算的XPath表達式。
  • disable-outputescaping - 默認 - no。 如果爲yes,則輸出文本不會從文本中轉義xml字符。

元素

  • 父元素 - xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:processing指令, xsl:template, xsl:variable, xsl:when, xsl:with-param輸出元素。

示例

此示例創建一個<student>元素的表格,其屬性爲rollno,其子元素:<firstname><lastname><nickname><marks>

文件:students.xml -

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?> 
<class> 
   <student rollno = "10010">
      <firstname>Machine</firstname> 
      <lastname>Lee</lastname> 
      <nickname>Machine</nickname> 
      <marks>85</marks>
   </student> 
   <student rollno = "10020"> 
      <firstname>Max</firstname> 
      <lastname>Su</lastname> 
      <nickname>Maxsu</nickname> 
      <marks>95</marks>
   </student> 
   <student rollno = "10030"> 
      <firstname>Yii</firstname> 
      <lastname>Bai</lastname> 
      <nickname>Yiibai</nickname> 
      <marks>90</marks> 
   </student> 
</class>

文件:students.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>Students</h2> 

            <table border = "1"> 
               <tr bgcolor = "#3498db"> 
                  <th>Roll No</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Marks</th> 
               </tr> 

               <!-- for-each處理指令
               查找與XPath表達式匹配的每個元素
               --> 

               <xsl:for-each select="class/student"> 
                  <tr> 
                     <td> 
                        <!-- value-of處理指令
                        處理與XPath表達式匹配的元素的值
                        --> 
                        <xsl:value-of select = "@rollno"/> 
                     </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 = "marks"/></td> 

                  </tr> 
               </xsl:for-each> 

            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

在瀏覽器中打上面文件:student.xml,效果如下所示 -

XSLT