XSLT <xsl:template> องค์ประกอบ

ການອະທິບາຍ ແລະ ການນໍາໃຊ້

<xsl:template> ປະກອບມີບົດລະບຽບ ທີ່ຈະນຳໃຊ້ເມື່ອຕິດຕໍ່ຕົວແທນ ຕົວຈາກຄວາມຄົບຄອບ.

ປະເພດ match ຖືກນຳໃຊ້ເພື່ອຕິດຕໍ່ຕົວແທນ XML. ປະເພດ match ກໍ່ສາມາດນຳໃຊ້ເພື່ອກໍານົດຕົວແທນ ສິ່ງທັງໝົດຂອງ XML ບັນນາດ (ອີງຕາມ match="/" ກໍານົດຕົວແທນ ບັນນາດທັງໝົດຂອງ ບັນນາດ XML).

ຄວາມຄົນ:<xsl:template> ແມ່ນປະເພດສູງສຸດ (top-level element).

ການຂຽນ

<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">
  <!-- Content:(<xsl:param>*,template) -->
</xsl:template>

ປະເພດ

ປະເພດ ຄວາມຄົນ ການອະທິບາຍ
ຄວາມຄົນ ຄວາມຄົນ

name

ຄວາມຄົນ: ຖ້າບໍ່ມີຫຼັກສັບສຳຫຼັບບົດລະບຽບ, ຕ້ອງໄດ້ຕັ້ງປະເພດ match.

match pattern

ຄວາມອິດສະຫຼະ. ບໍ່ມີຫຼັກສັບສຳຫຼັບບົດລະບຽບຕົວແທນ.

ຄວາມຄົນ: ຖ້າບໍ່ມີຫຼັກສັບສຳຫຼັບບັນດາບົດລະບຽບ, ຕ້ອງໄດ້ຕັ້ງປະເພດ name.

mode mode ຄວາມອິດສະຫຼະ. ບໍ່ມີຫຼັກສັບສຳຫຼັບຕົວແທນ.
priority number ຄວາມອິດສະຫຼະ. ບັນດາບົດລະບຽບຂອງຕົວແທນບໍ່ມີຫຼັກສັບ.

ຄວາມຄົນ

ບັນດາບົດລາຍລະອຽດ 1

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2> 
  <xsl:apply-templates/> 
  </body>
  </html>
</xsl:template>
<xsl:template match="cd">
  <p>
  <xsl:apply-templates select="title"/> 
  <xsl:apply-templates select="artist"/>
  </p>
</xsl:template>
<xsl:template match="title">
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
<xsl:template match="artist">
  Artist: <span style="color:#00ff00">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
</xsl:stylesheet>