2011-07-11 72 views
2

我有类似如下的XML文件:简单XML到HTML表格

<?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,我只需要这一次

+0

这是比较容易,如果提供一个答案您将张贴HTML,而不是呈现的HTML的图像。 –

+0

好问题,+1。查看我的答案,获得更强大的解决方案,它不仅更短,而且即使在'body'中的col'元素混合排列的情况下也能产生正确的结果。 –

回答

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> 

实际上,它将把一个样式属性上的每个<td><th>这意味着输出的有点冗长,但它一直在XSLT不错&简单。

+0

非常感谢!正是我需要的! – mcha

+0

NP。有更强大的方法可以做到这一点,但如果你不是真的进入XSLT,我认为最好尽可能简化它,而不是增加复杂性来解决那些不存在的问题。 – Flynn1179

-2

这可能听起来有点让人费解......但如何用查找和替换文本编辑器?

XSL是最好的解决方案,因为这是它的目的。

如果你到脚本,然后常规具有良好的XML处理和也许可以破解的解决方案为你

+0

如果可能的话XSL是的,我可以有超过100个从XML文件显示.. – mcha

-3

你的意思是XSL?

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

1

这短变换产生的通缉的结果,即使在bodycol元素不按照正确的顺序

-

<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是混合顺序):

-

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