XSLT 元素

علمی ایکسی ایلٹی <xsl:choose> عنصر استعمال کیا جاتا ہے تاکہ <xsl:when> اور <xsl:otherwise> کے ذریعہ متعدد شرطی امتحانات کو ظاہر کیا جاسکے。

<xsl:choose> عنصر

قواعد

<xsl:choose>
  <xsl:when test="expression">
    ... خروج ...
  </xsl:when>
  <xsl:otherwise>
    ... خروج ...
  </xsl:otherwise>
</xsl:choose>

چینس کو کہا جائے

XML فائل کیلئے کثیر شرطی ٹیسٹ داخل کرنے کیلئے XSL فائل میں <xsl:choose>، <xsl:when> اور <xsl:otherwise> شامل کریئے:

<?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>

مزید کریئن کا رنگ CD کی قیمت 10 سے زیادہ ہونے پر "Artist" سٹول میں پورپل پس منظر رنگ دینا ہوگا۔

نتیجه تبدیل مشابه زیر است:

اس XML فائل کو دیکھئے,اس XSL فائل کو دیکھئے,نتیجه را مشاهده کنید.

دوسرے مثال

یہ دوسرے <xsl:when> عناصر کا مثال ہے:

<?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:when test="price > 9">
            <td bgcolor="#cccccc">
            <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>

این کد در صورت اینکه قیمت CD بیشتر از 10 باشد، رنگ پس‌زمینه صورتی را به ستون "Artist" اضافه می‌کند و در صورت اینکه قیمت CD بین 9 و 10 باشد، رنگ پس‌زمینه خاکستری را به ستون "Artist" اضافه می‌کند.

نتیجه تبدیل مشابه زیر است:

این فایل XML را مشاهده کنید,این فایل XSL را مشاهده کنید,نتیجه را مشاهده کنید.