html - 简单的 XML 到 HTML 表格

标签 html xml xslt html-table

我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<mylist name="test">
    <title>--$title$- not found</title>
    <table>
        <header>
            <col name="firstname"      title="--$firstname$- not found"/>
            <col name="lastname"      title="--$lastname$- not found"/>
            <col name="country"        title="--$country$- not found"/>
      </header>
            <body>
                <row>
                <col name="firstname">John</col>
                <col name="lastname">Smith</col>
                <col name="country">ENGLAND</col>
              </row>
              <row>
              <col name="firstname">Peter</col>
                <col name="lastname">Scott</col>
                <col name="country">USA</col>
              </row>
        </body>
    </table>
</mylist> 

我需要将结果显示为如下所示的 HTML 表格:

enter image description here

有人可以帮忙吗?我不喜欢 XML/XSL,我只需要一次

最佳答案

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="mylist">
    <html>
      <xsl:apply-templates />
    </html>
  </xsl:template>

  <xsl:template match="title">
    <head>
      <title><xsl:value-of select="." /></title>
    </head>
  </xsl:template>

  <xsl:template match="table">
    <table style="border: 1px solid black">
      <xsl:apply-templates />
    </table>
  </xsl:template>

  <xsl:template match="header">
    <thead>
      <tr>
        <xsl:apply-templates />
      </tr>
    </thead>
  </xsl:template>

  <xsl:template match="body">
    <tbody>
      <xsl:apply-templates />
    </tbody>
  </xsl:template>

  <xsl:template match="row">
    <tr>
      <xsl:apply-templates />
    </tr>
  </xsl:template>

  <xsl:template match="col[parent::header]">
    <th style="border: solid black 1px;"><xsl:value-of select="@name" /></th>
  </xsl:template>

  <xsl:template match="col[parent::row]">
    <td style="border: solid black 1px;"><xsl:value-of select="." /></td>
  </xsl:template>
</xsl:stylesheet>

它实际上会在每个 <td> 上放置一个样式属性或 <th>这意味着输出有点冗长,但它使 XSLT 保持美观和简单。

关于html - 简单的 XML 到 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650230/

相关文章:

XSLT、循环中的多个 XML 文件、命名空间问题

html - Angular 2 : How do you pass a selected item from a HTML datalist element back to the component?

html - 高度 : auto; isn't working well

html - 为什么我的页脚没有显示在底部?这个 CSS 有什么问题?

python - GPXXMLSyntaxException : Error parsing XML: not well-formed (invalid token): line 1, 第 11 列

c# - 使用 LINQ 从 XML 中选择后代节点和元素

xml - 在 bash 中基于 XML 内容验证获取内容

xml - 如何在xslt中获取节点的命名空间?

php - xsl根据数值选择节点

javascript - isteven-multi-select 输出模型不适用于模板