XSLT <xsl:variable> อิเล็ม

การใช้งานและการประกาศ

<xsl:variable> ใช้สำหรับประกาศตัวแปรท้องถิ่นหรือทั่วไป.

หมายเหตุ:ถ้าได้มีการประกาศในตำแหน่งระดับสูง ตัวแปรนี้จะเป็นตัวแปรทั่วไป และถ้าประกาศในตัวแบบแบบนี้ ตัวแปรจะเป็นตัวแปรท้องถิ่น.

หมายเหตุ:หลังจากที่คุณได้ตั้งค่าค่าต่อตัวแปรแล้ว คุณจะไม่สามารถเปลี่ยนแปลงหรือแก้ไขค่านั้นได้!

คำเตือน:คุณสามารถเพิ่มค่าต่อตัวแปรด้วยเนื้อหาของ <xsl:variable> หรือด้วยค่าของ select ได้!

รูปแบบ

<xsl:variable
name="name"
select="expression">
  <!-- Content:template -->
</xsl:variable>

คุณสมบัติ

คุณสมบัติ ค่า รายละเอียด
name name สำคัญ
select expression ตัวเลือก

ตัวอย่าง

ตัวอย่าง 1

ถ้าได้ตั้งค่า select แล้ว ตัวแปร <xsl:variable> จะไม่สามารถมีเนื้อหาใดๆ ด้วย ถ้า select มีข้อความต้องใส่เครื่องหมายเชิญของข้อความ:

ตัวอย่างสองตัวด้านล่างใช้ค่าต่อตัวแปร "color" คือ "red":

<xsl:variable name="color" select="'red'" />
<xsl:variable name="color" select='"red"' />

ตัวอย่าง 2

ถ้า <xsl:variable> มีแค่ค่าที่มีชื่อแค่นั้น และไม่มีเนื้อหา ค่าต่อตัวแปรคือตัวว่าง:

<xsl:variable name="j" />

ตัวอย่าง 3

ตัวอย่างด้านล่างใช้ <xsl:variable> สำหรับจัดให้ค่าต่อตัวแปร "header":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="header">
  <tr>
  <th>Element</th>
  <th>รายละเอียด</th>
  </tr>
</xsl:variable>
<xsl:template match="/">
  <html>
  <body>
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="reference/record">
    <tr>
    <xsl:if category="XML">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  <br />
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="table/record">
    <tr>
    <xsl:if category="XSL">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>