핵심 태그
1. <xsl:value-of>
역할:지정한 XML 노드의 텍스트 내용을 추출하여 결과 문서에 출력합니다.
<!-- 첫 번째 catalog/cd의 title 값 가져오기 (반복문 없으면 첫 번째 데이터만 가져옴) -->
<xsl:value-of select="catalog/cd/title" />Code language: HTML, XML (xml)
<xsl:for-each> 반복문과 함께 사용하지 않으면 첫 번째 일치 노드만 가져오며 전체 데이터를 순회할 수 없습니다.
2. <xsl:for-each>
역할:조건에 일치하는 XML 노드 묶음을 순회하며 여러 데이터를 일괄 처리합니다.
<xsl:for-each select="class/student">
<li><xsl:value-of select="name"/>;<xsl:value-of select="age"/></li>
</xsl:for-each>Code language: HTML, XML (xml)
XPath 필터 문법
select 경로 뒤에 [조건]을 사용해 노드를 걸러내 데이터 필터링을 구현합니다
<!-- artist가 Bob Dylan인 cd 레코드만 출력 -->
<xsl:for-each select="catalog/cd[artist='Bob Tom']">Code language: HTML, XML (xml)
지원하는 연산자
| 연산자 | XPath 표기 | 의미 |
|---|---|---|
| = | = | 같다 |
| != | != | 다르다 |
| < | < | 작다(<를 직접 쓰지 말고 이스케이프 필수) |
| > | > | 크다(>를 직접 쓰지 말고 이스케이프 필수) |
예:가격이 10보다 작은 CD 필터링
<xsl:for-each select="catalog/cd[price < 10]">Code language: HTML, XML (xml)
이전 예제
student.xsl
<?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>
<head>
</head>
<body>
<ul>
<!-- 모든 student 노드 순회 -->
<xsl:for-each select="class/student">
<li><xsl:value-of select="name"/>;<xsl:value-of select="age"/></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>Code language: HTML, XML (xml)
연동 XML(student.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>
<class>
<student>
<name>Jack</name>
<age>16</age>
</student>
<student>
<name>Jim</name>
<age>12</age>
</student>
</class>Code language: HTML, XML (xml)
필터 추가
나이가 14세보다 큰 학생만 출력
<xsl:for-each select="class/student[age > 14]">
<li><xsl:value-of select="name"/>;<xsl:value-of select="age"/></li>
</xsl:for-each>Code language: HTML, XML (xml)
반복문