Latest web development tutorials

XSLT <XSL: เลือก> องค์ประกอบ

<xsl: เลือก> องค์ประกอบที่จะใช้ร่วม <XSL: เมื่อ> และ <XSL ฉะนั้น> เพื่อแสดงการทดสอบหลายเงื่อนไข


<xsl: เลือก> องค์ประกอบ

ไวยากรณ์

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


สถานที่ที่เกณฑ์การคัดเลือก

การแทรกไฟล์ XML สำหรับเงื่อนไขการทดสอบหลายเพิ่มไฟล์ XSL ก <XSL: เลือก>, <XSL: เมื่อ> และ <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 &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>

ลอง»

รหัสข้างต้นจะสูงกว่าราคาของซีดี 10 คอลัมน์เพิ่มสีพื้นหลังสีชมพู "ศิลปิน"


อีกตัวอย่างหนึ่ง

นี้เป็นอีกหนึ่งมีสอง <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 &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>

ลอง»

รหัสข้างต้นสูงกว่า 10 ถึง "ศิลปิน" เพื่อเพิ่มสีพื้นหลังสีชมพูในราคาของแผ่นซีดีและอื่น ๆ อีกกว่า 9 และต่ำกว่าราคาของแผ่นซีดีจะมีค่าเท่ากับ 10 "ศิลปิน" เพื่อเพิ่มสีพื้นหลังสีเทา