--- title: Generating HTML slug: XSLT_in_Gecko/Generating_HTML translation_of: Web/API/XSLTProcessor/Generating_HTML ---

HTML 생성하기

브라우저에서 XSLT의 공통 응용은 XML을 클라이언트의 안에 변환해 넣는 것이다. 두번째 예는 입력문서(example2.xml)를 변환하는데, 이것은 또 글의 정보를 포함하고 HTML문서 안에 들어간다.

Article의 <body> 요소는 지금 HTML의 요소 (<b><u> 태그, 그림 2)를 포함한다. XML 문서는 HTML요소와 XML 요소 모두 포함하지만, 단 하나의 namespace 즉 XML 요소만 필요하다. HTML namespace가 없으므로, 그리고 XHTML namespace를 사용하여 XSL에서 an XML document를 생성하고 그것은 HTML문서와 같은 양상은 아닐 것이고, XSL Stylesheet 의xsl:output는 결과문서는 HTML처럼 다루어질 것을 보장한다 . XML 요소에 대해, 우리 자신의 namespace http://devedge.netscape.com/2002/de 는 필요하고, 그것은 접두어 myNS (xmlns:myNS="http://devedge.netscape.com/2002/de)에 주어진다.

그림 2 XML 파일 (example2.xml) XML Document (example2.xml):

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example2.xsl"?>
<myNS:Article xmlns:myNS="http://devedge.netscape.com/2002/de">
<myNS:Title>My Article</myNS:Title>
<myNS:Authors>
<myNS:Author company="Foopy Corp.">Mr. Foo</myNS:Author>
<myNS:Author>Mr. Bar</myNS:Author>
</myNS:Authors>
<myNS:Body>
The <b>rain</b> in <u>Spain</u> stays mainly in the plains.
</myNS:Body>
</myNS:Article>

The XSL Stylesheet used will need to have two namespaces - one for the XSLT elements and one for our own XML elements used in the XML document. The output of the XSL Stylesheet is set to HTML by using the xsl:output element. By setting the output to be HTML and not having a namespace on the resulting elements (colored in blue), those elements will be treated as HTML elements.

그림 3 : 두 namespaces를 가진 XSL Stylesheet (example2.xsl) XSL Stylesheet (example2.xsl):

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myNS="http://devedge.netscape.com/2002/de">

<xsl:output method="html"/>
...
</xsl:stylesheet version="1.0">

A template matching the root node of the XML document is created and used to create the basic structure of the HTML page.

Figure 4 : Creating the basic HTML document XSL Stylesheet (example2.xsl):

...
<xsl:template match="/">
<html>

<head>

<title>
<xsl:value-of select="/myNS:Article/myNS:Title"/>
</title>

<style type="text/css">
.myBox {margin:10px 155px 0 50px; border: 1px dotted #639ACE; padding:0 5px 0 5px;}
</style>

</head>

<body>
<p class="myBox">
<span class="title">
<xsl:value-of select="/myNS:Article/myNS:Title"/>
</span> </br>

Authors: <br />
<xsl:apply-templates select="/myNS:Article/myNS:Authors/myNS:Author"/>
</p>

<p class="myBox">
<xsl:apply-templates select="//myNS:Body"/>
</p>

</body>

</html>
</xsl:template>
...

Three more xsl:template's are needed to complete the example. The first xsl:template is used for the author nodes, while the second one processes the body node. The third template has a general matching rule which will match any node and any attribute. It is needed in order to preserve the html elements in the XML document, since it matches all of them and copies them out into the HTML document the transformation creates.

그림 5 : 최종 3개 템플리트 XSL Stylesheet(example2.xsl):

...
<xsl:template match="myNS:Author">
-- <xsl:value-of select="." />

<xsl:if test="@company">
:: <b> <xsl:value-of select="@company" /> </b>
</xsl:if>

<br />
</xsl:template>

<xsl:template match="myNS:Body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
...

The final XSLT stylesheet looks as follows:

그림 6 : 최종 XSLT Stylesheetview example | view source XSL Stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myNS="http://devedge.netscape.com/2002/de">

<xsl:output method="html" />

<xsl:template match="/">
<html>

<head>

<title>
<xsl:value-of select="/myNS:Article/myNS:Title"/>
</title>

<style type="text/css">
.myBox {margin:10px 155px 0 50px; border: 1px dotted #639ACE; padding:0 5px 0 5px;}
</style>

</head>

<body>
<p class="myBox">
<span class="title">
<xsl:value-of select="/myNS:Article/myNS:Title"/>
</span> <br />

Authors: <br />
<xsl:apply-templates select="/myNS:Article/myNS:Authors/myNS:Author"/>
</p>

<p class="myBox">
<xsl:apply-templates select="//myNS:Body"/>
</p>

</body>

</html>
</xsl:template>

<xsl:template match="myNS:Author">
-- <xsl:value-of select="." />

<xsl:if test="@company">
:: <b> <xsl:value-of select="@company" /> </b>
</xsl:if>

<br />
</xsl:template>

<xsl:template match="myNS:Body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
{{ languages( { "en": "en/XSLT_in_Gecko/Generating_HTML" } ) }}