开发者

Easy XML to HTML table

I have an XML file like the following :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<mylist name="test">
    <title>--$title$- not found</title>
    <table>
        <header>
            <col name="firstname"      title="--$firstname$开发者_Go百科- 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> 

and I need to show the result as a HTML table like the following :

Easy XML to HTML table

Could anyone help please? I'm not into XML/XSL and I just need this once


Try this:

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

It will actually put a style attribute on each <td> or <th> which means the output's a little verbose, but it keeps the XSLT nice & simple.


This shorter transformation produces the wanted result even if the col elements in the body are not in the right order:

--

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

--

When applied on this document (essentially the provided XML document, but the col elements in the body are in mixed order):

--

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

the wanted, correct result is produced:

--

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


This might sound a bit flipent... but how about a text editor with find and replace?

XSL is the nicest solution as this is what it was built for.

If you're into scripting then groovy has good XML handling and can probably hack a solution out for you


You mean XSL?

I would avoid XSL if possible and use either your favorite programming language, FreeMarker or perhaps both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜