Технология XSLT
Шрифт:
new URL("http://localhost/stylesheet.xsl"), null);
// Выполняем преобразование
XMLDocumentFragment fragment =
(XMLDocumentFragment)doc.transformNode(stylesheet);
// Выводим результат
fragment.print(out);
}
catch (MalformedURLException mue) {}
catch (XSLException xsle) {}
// Закрываем выходящий поток
out.close;
}
}
В
doc
мы генерируем DOM-объект XML-документа. После того как все текстовые узлы и узлы элементов будут сгенерированы, документ, содержащийся в переменной doc
, примет приблизительно следующий вид. Листинг 9.24. XML-документ, сгенерированный в сервлете
<Request>
<General>
<ServerName>aphrodite.fzi.de</ServerName>
<ServerPort>80</ServerPort>
<RemoteAddr>127.0.0.1</RemoteAddr>
<Protocol>HTTP/1.1</Protocol>
<Method>GET</Method>
<RequestURI>/servlet/example1</RequestURI>
<QueryString>x=1&y=2&z=3&x=4&y=5&z=6
</QueryString>
</General>
<Param>
<z>3</z>
<z>6</z>
<y>2</y>
<y>5</y>
<x>1</x>
<x>4</x>
</Param>
<Session>
<v>4</v>
</Session>
<Cookie>
<content>apple jam</content>
<JServSessionIdroot>aaenbyjqc0</JServSessionIdroot>
</Cookie>
</Request>
После того как генерация документа завершена, к нему применяется преобразование
stylesheet.xsl
, которое создает его HTML-представление. Листинг 9.25. Преобразование stylesheet.xsl
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Request">
<html>
<head>
<title>Request information</title>
</head>
<body><xsl:apply-templates mode="table"/></body>
</html>
</xsl:template>
<xsl:template match="*" mode="table">
<h1><xsl:apply-templates select="." mode="header"/></h1>
<table><xsl:apply-templates mode="row"/></table>
</xsl:template>
<xsl:template match="General" mode="header">
<xsl:text>General information</xsl:text>
</xsl:template>
<xsl:template match="Param" mode="header">
<xsl:text>Request parameters</xsl:text>
</xsl:template>
<xsl:template match="Session" mode="header">
<xsl:text>Session parameters</xsl:text>
</xsl:template>
<xsl:template match="Cookie" mode="header">
<xsl:text>Cookies</xsl:text>
</xsl:template>
<xsl:template match="*" mode="row">
<tr>
<td><xsl:apply-templates select="." mode="name"/></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
<xsl:template match="*" mode="name">
<xsl:value-of select="name"/>
</xsl:template>
<xsl:template match="General/ServerName" mode="name">
<xsl:text>Server name</xsl:text>
</xsl:template>
<xsl:template match="General/ServerPort" mode="name">
<xsl:text>Server port</xsl:text>
</xsl:template>
<xsl:template match="General/RemoteAddr" mode="name">
<xsl:text>Remote address</xsl:text>
</xsl:template>
<xsl:template match="General/RequestURI" mode="name">
<xsl:text>Request URI</xsl:text>
</xsl:template>
<xsl:template match="General/QueryString" mode="name">
<xsl:text>Query string</xsl:text>
</xsl:template>
Поделиться с друзьями: