XSLT 변환 과정
XSLT 변환 과정
XSLT의 변환 과정은 다음과 같이 크게 두 가지 과정으로 이루어집니다.
1. 변환(Transformation) : XML 문서를 다른 구조의 XML 문서로 변환하는 과정입니다.
2. 서식 설정(Formatting) : XML 문서를 특정 데이터 구조의 비 XML 문서로 변환하는 과정입니다.
XSLT 변환
변환될 데이터를 가지고 있는 XML 문서와 변환에 대한 규칙을 가지고 있는 XSLT 문서만 있으면 변환을 진행할 수 있습니다.
XML 문서를 잘 구성된(well-formed) HTML 문서로 바꾸는 과정을 순서대로 살펴보겠습니다.
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<programming_languages>
<language>
<name>HTML</name>
<category>web</category>
<developer>W3C</developer>
<version status="working draft">5.1</version>
<priority rating="1">high</priority>
</language>
...
<language>
<name korean="파이썬">Python</name>
<category>application</category>
<developer>Python</developer>
<version status="stable">3.52</version>
<priority rating="4">middle</priority>
</language>
</programming_languages>
1 단계 : XSLT 문서 작성
xml_xslt_01.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h2 style="text-align:center">프로그래밍 언어</h2>
<table border="1" style="width: 50%; margin: auto">
<tr>
<th>이름</th>
<th>종류</th>
<th>개발사</th>
<th>버전</th>
<th>중요도</th>
</tr>
<xsl:for-each select="programming_languages/language">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="category"/></td>
<td><xsl:value-of select="developer"/></td>
<td><xsl:value-of select="version"/></td>
<td><xsl:value-of select="priority"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
2 단계 : XML 문서에 XSLT 문서를 연결
xml_xslt_01.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xml_xslt_01.xsl"?>
<programming_languages>
<language>
<name>HTML</name>
<category>web</category>
<developer>W3C</developer>
<version status="working draft">5.1</version>
<priority rating="1">high</priority>
</language>
<language>
<name>CSS</name>
<category>web</category>
<developer>W3C</developer>
<version status="stable">3.0</version>
<priority rating="3">middle</priority>
</language>
<language>
<name korean="자바">Java</name>
<category>application</category>
<developer>Oracle</developer>
<version status="stable">8.91</version>
<priority rating="2">high</priority>
</language>
<language>
<name korean="파이썬">Python</name>
<category>application</category>
<developer>Python</developer>
<version status="stable">3.52</version>
<priority rating="4">middle</priority>
</language>
</programming_languages>