2011-11-15 117 views
0

我有一个XML文件生成的使用javax.sql.rowset.WebRowSet.writeXml它看起来像:XSLT转换通用的XML

<metadata> 
This section has column properties like name/label etc 
</metadata> 
<data> 
<currentRow> 
     <columnValue>Ken</columnValue> 
     <columnValue>12</columnValue> 
     <columnValue>USA</columnValue> 
    </currentRow> 
</data> 

我想转换这个样子:

<Class> 
    <Student> 
     <name>Ken</name> 
     <ID>12</ID> 
     <location>USA</location> 
    </Student> 
</Class> 

我该怎么办转换?我需要将XML转换为HTML表格.

+0

所以只有一个'currentRow'元素和多个'columnValue'元素? –

+0

是的..这是正确的 – spectrum

回答

0

以下样式表会产生所需的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <Class> 
      <xsl:apply-templates select="/*/data/currentRow" /> 
     </Class> 
    </xsl:template> 
    <xsl:template match="currentRow"> 
     <Student> 
      <xsl:apply-templates select="columnValue" /> 
     </Student> 
    </xsl:template> 
    <xsl:template match="columnValue[1]"> 
     <name><xsl:apply-templates/></name> 
    </xsl:template> 
    <xsl:template match="columnValue[2]"> 
     <ID><xsl:apply-templates/></ID> 
    </xsl:template> 
    <xsl:template match="columnValue[3]"> 
     <location><xsl:apply-templates/></location> 
    </xsl:template> 
</xsl:stylesheet> 

注意:添加一个根节点到给定的源使其格式良好。

+0

这个工程!谢谢 – spectrum