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

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