프로그래밍/General

XML에 XSL(T)를 적용해보자!

Lou Park 2017. 12. 12. 21:09

students.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
    <student>
        <name>Jieun Park</name>
        <student_id>201406253</student_id>
    </student>
 
    <student>
        <name>John Doe</name>
        <student_id>201304238</student_id>
    </student>
 
    <student>
        <name>Jane Doe</name>
        <student_id>201304238</student_id>
    </student>
</students>
cs

students.xsl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" doctype-system="about:legacy-compat"/>
    <xsl:template match="/">
    <html>
        <head>
            <meta charset="utf-8"/>
            <title>Students</title>
            <style type="text/css">
                td {
                    color: blue;
                }
            </style>
        </head>
 
        <body>
            <table>
            <caption>Information about various students</caption>
                <thead>
                    <tr>
                        <th>이름</th>
                        <th>학번</th>
                    </tr>
                </thead>
                <xsl:for-each select="/students/student">
                    <tr>
                        <td><xsl:value-of select="name"/></td<!-- Attribute는 @을 앞에 붙이세요.-->
                        <td><xsl:value-of select="student_id"/></td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
    </xsl:template>
</xsl:stylesheet>
cs