XSLT <XSL: ถ้า> องค์ประกอบ
ความหมายและการใช้งาน
<xsl: if> องค์ประกอบที่มีแม่แบบเฉพาะในกรณีที่เงื่อนไขที่ระบุไว้ก่อนที่คุณจะใช้แม่แบบนี้
แนะนำ: <XSL: เลือก> และ <XSL: เมื่อ> และ <XSL ฉะนั้น> เพื่อแสดงหลายเงื่อนไขที่ใช้ร่วมกับการทดสอบ!
ไวยากรณ์
<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>
test="expression">
<!-- Content: template -->
</xsl:if>
คุณสมบัติ
属性 | 值 | 描述 |
---|---|---|
test | expression | 必需。规定要测试的条件。 |
ตัวอย่าง
เมื่อเป็นราคาที่สูงกว่า 10 ซีดีเลือกมูลค่าของชื่อและศิลปินของ:
ตัวอย่างที่ 1
<?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>
<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>
ลอง»
แสดงชื่อของแต่ละแผ่นซีดี ถ้ามันไม่ได้เป็นครั้งสุดท้ายหรือสุดท้าย CD, CD-ชื่อระหว่างแต่ละแทรก "." หากซีดีที่ผ่านมาแล้วเพิ่มหลังจากหัวข้อ "!." ถ้าเป็นซีดีสุดท้ายแล้วหลังจากที่เพิ่มชื่อ "และ":
ตัวอย่างที่ 2
<?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>
<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>
ลอง»
คู่มือการใช้งาน XSLT องค์ประกอบอ้างอิง