如何转换XML到HTML表格

月夜之吻
发布: 2025-09-25 12:59:01
原创
289人浏览过
如何转换xml到html表格
<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在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号