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>


여기서 선택 기준을 놓고

XSL 파일에 추가, 여러 시험 조건에 대한 XML 파일을 삽입하려면 <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 열은 "아티스트"에 분홍색 배경 색상을 추가 CD의 가격보다 높을 것이다.


또 다른 예

이것은 또 다른 두 가지가 포함되어있다 <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 "아티스트"열이 CD의 가격에 분홍색 배경 색상을 추가하고, 이상 (9)와 CD의 가격보다 적은 회색 배경 색상을 추가 10 "아티스트"열와 동일하다.