Sau đây chúng ta sẽ xem ví dụ chuyển đổi XML sang HTML bằng XSLT. Chúng tôi cũng có thể sử dụng mã Java để chuyển đổi XML sang HTML nhưng điều đó sẽ yêu cầu nhiều LoC để hoàn thành công việc nhưng sử dụng XSLT thì việc chuyển đổi khá dễ dàng. XSLT là viết tắt của Biến đổi XSL
Ngôn ngữ biểu định kiểu mở rộng [XSL] là một nhóm các đề xuất và ngôn ngữ tạo kiểu để xác định cách trình bày và chuyển đổi tài liệu XML. Nó bao gồm ba phần
- Chuyển đổi XSL [XSLT] . một ngôn ngữ để chuyển đổi XML;
- Ngôn ngữ đường dẫn XML [XPath] . một ngôn ngữ biểu thức được sử dụng bởi XSLT [và nhiều ngôn ngữ khác] để truy cập hoặc tham chiếu đến các phần của tài liệu XML;
- Đối tượng định dạng XSL [XSL-FO] . một từ vựng XML để chỉ định ngữ nghĩa định dạng.
bài viết liên quan
- Cách xóa không gian tên khỏi XML bằng XSLT
Wiki cho biết tài liệu gốc không bị thay đổi; . Thông thường, tài liệu đầu vào là các tệp XML, nhưng bất kỳ thứ gì mà bộ xử lý có thể xây dựng Mô hình dữ liệu XQuery và XPath đều có thể được sử dụng, chẳng hạn như bảng cơ sở dữ liệu quan hệ hoặc hệ thống thông tin địa lý
Mặc dù XSLT được thiết kế như một ngôn ngữ có mục đích đặc biệt để chuyển đổi XML, nhưng ngôn ngữ này là Turing hoàn chỉnh, khiến nó có khả năng tính toán tùy ý về mặt lý thuyết
Hãy xem cách sử dụng XSLT để chuyển đổi tài liệu XML thành HTML
điều kiện tiên quyết
Nhật thực 2020-06, ít nhất là Java 1. 8, Kiến thức về HTML & XML
Thiết lập dự án
Tạo dự án dựa trên gradle hoặc maven trong Eclipse. Tên của dự án là java-xslt-xml-to-html
Nếu bạn đang tạo dự án dựa trên lớp trong Eclipse thì bạn có thể sử dụng bản dựng bên dưới. kịch bản lớp
plugins {
id 'java-library'
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
jcenter[]
}
dependencies {
}
Nếu bạn đang tạo dự án dựa trên maven thì bạn có thể sử dụng pom bên dưới. tập tin xml
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
Tệp XML
Bây giờ hãy đặt tệp XML bên dưới books.xml
trong thư mục src/main/resources/xml
Trong tệp XML bên dưới, bạn có thể thấy chúng tôi có rất nhiều dữ liệu có thể dễ dàng hiển thị trên tệp HTML ở định dạng bảng
Ở đây chúng tôi có danh mục nút gốc và dưới đây, chúng tôi có một số nút sách. Chúng tôi có id cuốn sách như một thuộc tính trên nút cuốn sách. Chúng ta cũng sẽ xem cách trích xuất thuộc tính id này bằng XSLT
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
2000-10-01
An in-depth look at creating applications
with XML.
Ralls, Kim
Midnight Rain
Fantasy
5.95
2000-12-16
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
Corets, Eva
Maeve Ascendant
Fantasy
5.95
2000-11-17
After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.
Corets, Eva
Oberon's Legacy
Fantasy
5.95
2001-03-10
In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.
Corets, Eva
The Sundered Grail
Fantasy
5.95
2001-09-10
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
Randall, Cynthia
Lover Birds
Romance
4.95
2000-09-02
When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.
Thurman, Paula
Splish Splash
Romance
4.95
2000-11-02
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
Knorr, Stefan
Creepy Crawlies
Horror
4.95
2000-12-06
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
Kress, Peter
Paradox Lost
Science Fiction
6.95
2000-11-02
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
O'Brien, Tim
Microsoft .NET: The Programming Bible
Computer
36.95
2000-12-09
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
O'Brien, Tim
MSXML3: A Comprehensive Guide
Computer
36.95
2000-12-01
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
Galos, Mike
Visual Studio 7: A Comprehensive Guide
Computer
49.95
2001-04-16
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
Tệp XSLT
Tiếp theo, tạo tệp XSLT có tên là
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
0 và đặt nó trong thư mục
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
1Ở đây, định dạng chuẩn của XSLT là giữ mọi thứ bên trong thẻ
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
2. Bạn cũng cần chỉ định không gian tên
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
0 cho XSLT. Sau đó, chúng tôi có thẻ
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
1 khớp với danh mục nút gốc và bắt đầu xử lý từ nút gốc nàyTiếp theo, chúng tôi muốn chọn dữ liệu XML và hiển thị thành định dạng HTML. Đó là lý do tại sao chúng tôi đã sử dụng các thẻ HTML ở đây. Chúng tôi cũng đã sử dụng một số css để tạo kiểu cho dữ liệu hàng thay thế. Sau đó, chúng tôi đang lặp lại từng nút [sách] và chọn các giá trị
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width:
100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child[even] {
background-color: #dddddd;
}
Books
Id
Author
Title
Genre
Price
Publish Date
Description
Chuyển đổi XML sang HTML
Viết lớp Java để chuyển đổi dữ liệu tệp XML sang HTML bằng tệp XSLT. Chúng tôi đã đặt cả tệp XML và XSLT trong đường dẫn lớp và cuối cùng chuyển đổi dữ liệu XML thành đầu ra HTML. Chúng tôi ghi đầu ra vào tệp HTML có tên là
4.0.0
com.roytuts
java-xslt-xml-to-html
0.0.1-SNAPSHOT
jar
UTF-8
at least 1.8
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
2 trong thư mục gốc của dự ánpackage com.roytuts.java.xslt.xml.to.html;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XmlToHtmlTransformer {
public static void main[String[] args] throws SAXException, IOException, ParserConfigurationException,
TransformerFactoryConfigurationError, TransformerException {
transform["xml/books.xml", "xslt/Xslt2Html.xsl"];
}
public static void transform[final String xml, final String xslt] throws SAXException, IOException,
ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException {
ClassLoader classloader = XmlToHtmlTransformer.class.getClassLoader[];
InputStream xmlData = classloader.getResourceAsStream[xml];
URL xsltURL = classloader.getResource[xslt];
Document xmlDocument = DocumentBuilderFactory.newInstance[].newDocumentBuilder[].parse[xmlData];
Source stylesource = new StreamSource[xsltURL.openStream[], xsltURL.toExternalForm[]];
Transformer transformer = TransformerFactory.newInstance[].newTransformer[stylesource];
StringWriter stringWriter = new StringWriter[];
transformer.transform[new DOMSource[xmlDocument], new StreamResult[stringWriter]];
// write to file
File file = new File["books.html"];
if [!file.exists[]] {
file.createNewFile[];
}
FileWriter fw = new FileWriter[file];
BufferedWriter bw = new BufferedWriter[fw];
bw.write[stringWriter.toString[]];
bw.close[];
}
}
Kiểm tra ứng dụng
Chạy lớp chính ở trên, bạn sẽ tìm thấy sách. html trong thư mục gốc của dự án
Bây giờ hãy mở những cuốn sách. tệp html trong Trình duyệt web trong Eclipse. Bạn sẽ thấy dữ liệu XML cuối cùng ở định dạng bảng trên Trình duyệt Web Eclipse. Bạn cũng có thể mở tệp HTML đầu ra trong trình duyệt