Sau đó ta tạo một file xsl với tên OrderProcessing như dưới:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : OrderProcessing.xsl
Created on : August 10, 2014, 3:04 PM
Author : Nguyen Minh An
Description:
Purpose of transformation follows.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>OrderProcessing.xsl</title>
</head>
<body>
<table border="1">
<tr>
<td>Name</td>
<td>Address</td>
</tr>
<xsl:apply-templates></xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="EmployeeList">
<xsl:apply-templates select="Employee">
<xsl:sort select="Name" order="ascending" data-type="text" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Employee">
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="Address"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Tiếp đó tạo ra một file xml như dưới:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<EmployeeList>
<Employee>
<Name>nguyen van0</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van1</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van2</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van3</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van4</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van5</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van6</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van7</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van8</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van9</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van10</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van11</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van12</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van13</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van14</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van15</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van16</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van17</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van18</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van19</Name>
<Address>address1</Address>
</Employee>
</EmployeeList>
Tiếp đó ở hàm main ta khai báo như sau:
public static void main(String[] args) {
try {
String orderProcessing = "D:\\NetBean\\Module 8\\src\\OrderProcessing.xsl";
String employee = "D:\\NetBean\\Module 8\\src\\Employee.xml";
String resultHtml = "D:\\NetBean\\Module 8\\src\\Result.html";
StreamSource sourceStyle = new StreamSource(orderProcessing);
StreamSource sourceXML = new StreamSource(employee);
StreamResult result = new StreamResult(new FileOutputStream(resultHtml));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transform = factory.newTransformer(sourceStyle);
transform.transform(sourceXML, result);
} catch (TransformerException | FileNotFoundException ex) {
Logger.getLogger(Module8.class.getName()).log(Level.SEVERE, null, ex);
}
}
Sau khi chạy ta được kết quả như sau:
Không có nhận xét nào:
Đăng nhận xét