XSLT <xsl:if> 요소

<xsl:if> 요소는 XML 파일 내용에 대한 조건 검사를 수행하는 데 사용됩니다。

<xsl:if> 요소

XML 파일 내용에 대한 조건 검사를 추가하려면, XSL 문서에 <xsl:if> 요소를 추가하세요。

문법

<xsl:if test="expression">
  ...
  ...조건이 성립되면 출력...
  ...
</xsl:if>

<xsl:if> 요소를 어디에 두는지

추가적인 조건 검사를 추가하려면, XSL 파일의 <xsl:for-each> 요소 내에 <xsl:if> 요소를 추가하세요:

<?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>

주석:필수 사항 test 속성의 값에는 평가할 필요가 있는 표현식이 포함됩니다.

위의 코드는 가격이 10보다 높은 CD의 title과 artist 요소만 출력합니다.

위의 변환 결과는 이렇게 보입니다:

이 XML 파일을 확인,이 XSL 파일을 확인,결과 확인.