XSLT <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>저의 CD 컬렉션</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>
예제 2
각 CD의 제목을 표시합니다. 마지막이거나 마지막 두 번째 CD가 아니라면, 각 CD-title 사이에 ", "를 삽입합니다. 마지막 CD라면, 제목 뒤에 "!"를 추가합니다. 마지막 두 번째 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>저의 CD 컬렉션</h2> <p>제목: <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>