XSLT <xsl:if> 元素

ກໍານົດ ແລະ ການນໍາໃຊ້

<xsl:if> ບັນຈຸຕົວຢ່າງ, ເພື່ອນຳໃຊ້ຕົວຢ່າງນັ້ນພຽງແຕ່ຂໍ້ຫວງຕິດຕາມກໍ່.

ຄຳແນະນຳ: ກະນາດານໃຊ້ <xsl:choose> ກັບ <xsl:when> ແລະ <xsl:otherwise> ເພື່ອສະແດງການຕິດຕາມຂໍ້ຫວງຫຼາຍຢ່າງ!

ກົດລະບຽບ

<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>

ປະກອບ

ປະກອບ ຄວາມມູນ ອະທິບາຍ
test expression ກໍານົດເງື່ອນໄຂທີ່ຈະຕິດຕາມ.

ຄວາມຈຳເປັນ

ຕົວຢ່າງ 1

ຖ້າລາຄາ CD ມີຫຼາຍກວ່າ 10, ຄັດເລືອກຄວາມມູນຄ່າ title ແລະ artist:

<?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>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:if test="price > 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

ກວດສອບເອກະສານ XMLກວດສອບເອກະສານ XSLກວດສອບຜົນ

ຕົວຢ່າງ 2

ສະແດງຫົວຂໍ້ CD ທັງໝົດ. ຖ້າບໍ່ແມ່ນ CD ສຸດທ້າຍຫຼືສຸດທ້າຍສອງ, ກຳລັງໃນລະຫວ່າງ CD-title ພຽງແຕ່ ", ". ຖ້າແມ່ນ CD ສຸດທ້າຍ, ກຳລັງຫຼັງຫົວຂໍ້ວ່າ "!". ຖ້າແມ່ນ CD ສຸດທ້າຍສອງ, ກຳລັງຫຼັງສຸດທ້າຍສອງວ່າ ", and ":

<?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>
    <p>Titles:
    <xsl:for-each select="catalog/cd">
      <xsl:value-of select="title"/>
      <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()-1">
        <xsl:text> and </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()">
        <xsl:text>!</xsl:text>
      </xsl:if>
    </xsl:for-each>
    </p>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>