XSLT <xsl:if>
❮ XSLT 요소 참조
정의 및 사용
<xsl:if> 요소는 지정된 조건이 true인 경우에만 적용되는 템플릿을 포함합니다.
팁: 여러 조건부 테스트를 표현하려면 <xsl:when> 및 <xsl:otherwise>와 함께 <xsl:choose>를 사용하십시오!
통사론
<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>
속성
Attribute | Value | Description |
---|---|---|
test | expression | Required. Specifies the condition to be tested |
예
CD 가격이 10보다 높은 경우 제목 및 아티스트 값을 선택합니다.
실시예 1
<?xml version="1.0" encoding="UTF-8"?>
<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가 아니거나 마지막 CD가 아닌 경우 각 CD 제목 사이에 ", "를 삽입합니다. 마지막 CD 인 경우 "!"를 추가하십시오. 제목 뒤에. 마지막 CD인 경우 제목 뒤에 " 및 "를 추가합니다.
실시예 2
<?xml version="1.0" encoding="UTF-8"?>
<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 요소 참조