عنصر <xsl:choose> لـ XSLT
- الصفحة السابقة XSLT <if>
- الصفحة التالية XSLT Apply
عنصر <xsl:choose> في XSLT يستخدم لتجميع <xsl:when> و <xsl:otherwise> لتمثيل اختبارات متعددة.
<xsl:choose> العنصر
النحو
<xsl:choose> <xsl:when test="عبارة"> ... إخراج ... </xsl:when> <xsl:otherwise> ... إخراج ... </xsl:otherwise> </xsl:choose>
أين يتم وضع شرط الاختيار
لإضافة اختبارات متعددة للملف XML، قم بإضافة <xsl:choose>،<xsl:when> و <xsl:otherwise> إلى ملف XSL:
<?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>
الكود المذكور أعلاه سيضيف لون خلفية برتقالي لسطر "Artist" عند أن يكون سعر CD أعلى من 10.
نتيجة التحويل أعلاه تشبه هذا:

مثال آخر
هذا مثال آخر يحتوي على عناصر <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>
الكود أعلاه سيضيف لون خلفي باللون الأحمر إلى عمود "Artist" عند تجاوز سعر CD 10، وسيضيف لون خلفي باللون الرمادي عند تجاوز سعر CD 9 ولكن دون أن يصل إلى 10.
نتيجة التحويل أعلاه تشبه هذا:

- الصفحة السابقة XSLT <if>
- الصفحة التالية XSLT Apply