简单的XML到HTML表格

mch*_*cha 2 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> 
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

有人可以帮忙吗?我没有进入XML/XSL,我只需要一次

Fly*_*179 5

试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

它实际上会在每个上添加一个样式属性,<td>或者<th>意味着输出有点冗长,但它使XSLT保持简洁.