
<books>
<book id="bk101">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book id="bk102">
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<year>1997</year>
<price>25.00</price>
</book>
</books><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/books">
<html>
<head>
<title>图书列表</title>
<style>
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>我的图书收藏</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版年份</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="book">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="year"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, "text/xml");
const table = document.createElement('table');
// ... 创建表头 ...
const books = xmlDoc.getElementsByTagName("book");
for (let i = 0; i < books.length; i++) {
const book = books[i];
const row = table.insertRow();
row.insertCell().textContent = book.getAttribute('id');
row.insertCell().textContent = book.getElementsByTagName('title')[0].textContent;
// ... 插入其他单元格 ...
}
document.getElementById('output').appendChild(table);import xml.etree.ElementTree as ET
xml_string = "<books>...</books>" # 你的XML数据
root = ET.fromstring(xml_string)
html_output = "<table><thead>...</thead><tbody>"
for book in root.findall('book'):
html_output += "<tr>"
html_output += f"<td>{book.get('id')}</td>"
html_output += f"<td>{book.find('title').text}</td>"
# ... 其他字段 ...
html_output += "</tr>"
html_output += "</tbody></table>"
# 将html_output发送给客户端以上就是如何转换XML到HTML表格的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号