<xsl:choose> به همراه <xsl:when> و <xsl:otherwise> برای ایجاد انتخاب تگ  با چندین شرط به کار می رود اگر شما با زبانهای برنامه نویسی مثل vb اشنا باشید می توان گفت که این دستورات مثل select case , case else می باشد خود دستور select case در اینجا می شود choose شما به تعداد حالتهای مورد نظرتان case می گذارید که در اینجا ما به جای Case عبارت when را داریم و به جای case else در اینجا ما otherwise را داریم
اگر با زبان php  اشنایی دارید می توان دستورات choose را مثل دستور switch و دستور when را مثل مجموعه case ها و دستور otherwise را مثل دستور default در نظر گرفت که when  ها و otherwise باید درون دستور choose باشند

 روش استفاده از این دستور به صورت زیر است

<xsl:choose>
<xsl:when test=”expression”>
… some output …
</xsl:when>
<xsl:otherwise>
… some output ….
</xsl:otherwise>
</xsl:choose>

 مثال

<?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 &gt; 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>

بیایید خطهای بین for-each را بررسی کنیم دستورات زیر درون حلقه for-each هستند و مدام تکرار می شوند

<tr>
<td><xsl:value-of select=”title”/></td>
<xsl:choose>
<xsl:when test=”price &gt; 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>

ابتدا یک سطر جدید به جدول افزوده می شود سپس مقدار تگ title بعدی در سلول این سطر قرار می گیرد دستور شرطی choose اجرا می شود

دستور شرطی choose دارای دستورات زیر است:

<xsl:when test=”price &gt; 10″>
<td bgcolor=”#ff00ff”>
<xsl:value-of select=”artist”/></td>
</xsl:when>

<xsl:otherwise>
<td><xsl:value-of select=”artist”/></td>
</xsl:otherwise>

دستور when می گوید اگر مقدار تگ price ای که در مسیر تگ title فعلی است بیشتر از ۱۰ بود انگاه در سطر جاری رنگ زمینه سلول دومی  صورتی شود و محتوای تگ artist ای که با تگ title هم مجموعه است یعنی با هم در یک تگ cd هستند در ان نشان داده شود در غیر این صورت فقط مقدار تگ artist در سلول قرار گیرد و رنگ زمینه سلول تغییر نکند

مثال دیگر
در اینجا ما از دو تا دستور 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 &gt; 10″>
<td bgcolor=”#ff00ff”>
<xsl:value-of select=”artist”/></td>
</xsl:when>
<xsl:when test=”price &gt; 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>

فقط قسمت choose را بررسی می کنم بقیه موارد قبلا در فصلهای قبلی کاملا توضیح داده شده است

<xsl:choose>
<xsl:when test=”price &gt; 10″>
<td bgcolor=”#ff00ff”>
<xsl:value-of select=”artist”/></td>
</xsl:when>

<xsl:when test=”price &gt; 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>

دستور when اول :

<xsl:when test=”price &gt; 10″>
<td bgcolor=”#ff00ff”>
<xsl:value-of select=”artist”/></td>
</xsl:when>

 می گوید اگر مقدار محتوای تگ price که در مجموعه تگ title قرار دارد بیشتر از ۱۰ بود انگاه سلول ایجاد شده رنگ زمینه به شماره ff00ff داشته باشد و مقدار محتوای تگ artist که با تگ price ان هم خانواده است در این سلول نشان داده شود

مشخص است که اگر شرط when برقرار نبود ای دستور اجرا نخواهد شد

دستور when دوم

<xsl:when test=”price &gt; 9″>
<td bgcolor=”#cccccc”>
<xsl:value-of select=”artist”/></td>
</xsl:when>

 می گوید اگر مقدار محتوای تگ price که در مجموعه تگ title قرار دارد بیشتر از ۹ بود انگاه سلول ایجاد شده رنگ زمینه به شماره cccccc داشته باشد و مقدار محتوای تگ artist که با تگ price ان هم خانواده است در این سلول نشان داده شود
مشخص است که اگر شرط when برقرار نبود ای دستور اجرا نخواهد شد

در غیر این صورت یعنی اگر هیچکدام از when ها  درست نبودند دستورات دورن otherwise اجرا می شود

<xsl:otherwise>
<td><xsl:value-of select=”artist”/></td>
</xsl:otherwise>

 و سلول بدون اینکه زمینه اش رنگی شود مقدار محتوای تگ artist را نمایش می دهد ببینید وقتی می گویم تگ های titile , price , artist  هم خانواده یعنی چی ؟

فرض کنید دستورات زیر قسمتی از یک برنامه کامل xml باشد

<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>

 در این جا ما دو مجموعه تگ cd داریم هر تگ cd می شود پدر برای تگ های دورن خود پس تگ های titile , artist , price , year در هر تگ cd می شوند هم خانواده یا برادر و خواهر  هر مجموعه هیچ ربطی به مجموعه دیگر ندارد