【问题标题】:Easy XML to HTML table简单的 XML 到 HTML 表格
【发布时间】:2011-07-11 12:42:14
【问题描述】:

我有一个如下的 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 表格,如下所示:

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

【问题讨论】:

  • 如果您要发布 HTML,而不是呈现的 HTML 的图像,提供答案会更容易。
  • 好问题,+1。请参阅我的答案以获得更强大的解决方案,该解决方案不仅更短,而且即使在body 中的col 元素处于混合顺序的情况下也会产生正确的结果。

标签: html xml xslt html-table


【解决方案1】:

试试这个:

<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>

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

【讨论】:

  • NP。有更强大的方法可以做到这一点,但如果你不是真的喜欢 XSLT,我认为最好让它尽可能简单,而不是增加复杂性来解决不存在的问题。
【解决方案2】:

即使body 中的col 元素的顺序不正确,这种较短的转换也会产生所需的结果

--

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vColNames">
  <xsl:text>|</xsl:text>
  <xsl:for-each select="/*/*/header/col">
    <xsl:value-of select="concat(@name, '|')"/>
   </xsl:for-each>
 </xsl:variable>

 <xsl:template match="table">
     <table border="1">
      <xsl:apply-templates/>
     </table>
 </xsl:template>

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

 <xsl:template match="header/col">
  <td><xsl:value-of select="@name"/></td>
 </xsl:template>

 <xsl:template match="body/row">
  <tr>
   <xsl:apply-templates select=
    "col[contains($vColNames,concat('|',@name, '|'))]">
     <xsl:sort select=
     "string-length(substring-before($vColNames, @name))"/>
   </xsl:apply-templates>
  </tr>
 </xsl:template>

 <xsl:template match="col">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

--

应用于此文档时(本质上是提供的 XML 文档,但 body 中的 col 元素是混合顺序的):

--

<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="lastname">Smith</col>
                <col name="firstname">John</col>
                <col name="country">ENGLAND</col>
            </row>
            <row>
                <col name="country">USA</col>
                <col name="firstname">Peter</col>
                <col name="lastname">Scott</col>
            </row>
        </body>
    </table>
</mylist>

产生想要的正确结果

--

<table border="1">
   <thead>
      <tr>
         <td>firstname</td>
         <td>lastname</td>
         <td>country</td>
      </tr>
   </thead>
   <tr>
      <td>John</td>
      <td>Smith</td>
      <td>ENGLAND</td>
   </tr>
   <tr>
      <td>Peter</td>
      <td>Scott</td>
      <td>USA</td>
   </tr>
</table>

【讨论】:

    【解决方案3】:

    这听起来可能有点轻率……但是带有查找和替换功能的文本编辑器怎么样?

    XSL 是最好的解决方案,因为它就是为此而构建的。

    如果您喜欢编写脚本,那么 groovy 具有良好的 XML 处理能力,并且可能会为您提供解决方案

    【讨论】:

    • 如果可能的话 XSL 是的,我可以从 xml 文件中显示超过 100 行..
    【解决方案4】:

    你是说 XSL?

    如果可能,我会避免使用 XSL,并使用您最喜欢的编程语言、FreeMarker 或两者兼而有之。

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2011-03-17
      • 2016-06-11
      相关资源
      最近更新 更多