XSLT <xsl:choose> ປ່ຽນ

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

<xsl:choose> ປະກອບກັບ <xsl:when> ແລະ <xsl:otherwise> ຈະສະແດງການກວດກາສັນຍານຫຼາຍບ່ອນ.

ຖ້າ <xsl:when> ບໍ່ແມ່ນ true ຈະກະທຳຂອງ <xsl:otherwise>.

如果没有 是 true,且不存在 元素,则不创建任何内容。

ຄຳແນະນຳ:ສຳລັບການພິສູດຄວາມຫຼາຍບ່ອນທີ່ສະເພາະຫຼາຍຄວາມຈະນໍາໃຊ້ <xsl:if> ແທນ.

ຄຳນວຍຄວາມ

<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>

ຄວາມປະສົງ

None

ຄວາມສະເພາະ

ກໍານົດ 1

ລະຫັດການທີ່ຈະສາມາດພິສູດຄວາມສູງຂອງ CD ກວ່າ 10 ແມ່ນຈະສ້າງພາບຫຼັງໃຫ້ບັນດາຜູ້ສະແດງໃນບັນດາວັດຖຸ 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">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

ຊອກຫາບັນດາສະໜາມ XML, XSL, ແລະຜົນການກະທຳ.

ກໍານົດ 2

ປະກາດຊີວິດທີ່ "color"。ຕິດຕັ້ງຄູ່ມູນຂອງຊີວິດນັ້ນໃຫ້ຜົນຂອງບັນດາປ້ອງກັນຄອມເພລັກ current ທີ່ມີການຕິດຕັ້ງຄວາມຜະ່ັດ color: ຖ້າປ້ອງກັນຄອມເພລັກ current ບໍ່ມີການຕິດຕັ້ງຄວາມຜະ່ັດ color ຈະເປັນ "green":

<xsl:variable name="color">
  <xsl:choose>
    <xsl:when test="@color">
      <xsl:value-of select="@color"/>
    </xsl:when>  
    <xsl:otherwise>green</xsl:otherwise>
  </xsl:choose>
</xsl:variable>