ایکس ایس ایل <xsl:if> عنصر
تعریف و استفاده
<xsl:if> شامل یک قالب است که تنها زمانی اعمال میشود که شرط مشخص شده صادق باشد.
توضیح: لطفاً از <xsl:choose> با <xsl:when> و <xsl:otherwise> استفاده کنید تا تستهای چندگانه را بیان کنید!
نحوهی نوشتن
<xsl:if test="expression"> <!-- Content: template --> </xsl:if>
ویژگی
ویژگی | مقدار | توضیح |
---|---|---|
test | expression | ضروری. شرطی که میخواهید تست کنید را مشخص کنید. |
مثال
مثال 1
وقتی قیمت CD از 10 بیشتر باشد، مقادیر title و 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"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
فایل XML را مشاهده کنید,فایل XSL را مشاهده کنید,نتیجه را مشاهده کنید.
مثال 2
نمایش عنوان هر CD. اگر CD آخرین یا دومین به آخر نباشد، پس از هر CD-title یک ", " بین آنها قرار دهید. اگر CD آخرین باشد، پس از عنوان یک "!" اضافه کنید. اگر دومین به آخر باشد، پس از آن یک ", and " اضافه کنید:
<?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> <p>Titles: <xsl:for-each select="catalog/cd"> <xsl:value-of select="title"/> <xsl:if test="position()!=last()"> <xsl:text>, </xsl:text> </xsl:if> <xsl:if test="position()=last()-1"> <xsl:text> and </xsl:text> </xsl:if> <xsl:if test="position()=last()"> <xsl:text>!</xsl:text> </xsl:if> </xsl:for-each> </p> </body> </html> </xsl:template> </xsl:stylesheet>