2012-01-12 88 views
2

我想通过TBODY创建分开由THEAD和数据行标题行的表结构:XSLT +创建表结构

输入XML:

<Rowsets> 
    <Rowset> 
    <Columns> 
     <Column Description="Date"/> 
     <Column Description="Time"/> 
    </Columns> 
    <Row> 
     <Date>DATA1</Date> 
     <Time>DATA2</Time> 
    </Row> 
    <Row> 
     <Date>DATA1</Date> 
     <Time>DATA2</Time> 
    </Rowset> 
</Rowsets> 

以下XSLT确实会分隔标题和正文,但我无法弄清楚如何在数据行之间打包标记:

<xsl:strip-space elements="*"/> 
<xsl:template match="/"> 
    <HTML> 
    <BODY> 
     <TABLE> 
     <XSL:apply-templates/> 
     </TABLE> 
    </BODY> 
    </HTML> 
</xsl:template> 

<xsl:template match="Columns|Row"> 
    <tr><xsl:apply-templates/></tr> 
</xsl:template> 

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

<xsl:template match="Columns/*"> 
    <th><xsl:apply-templates select="@Description"/></th> 
</xsl:template> 

<xsl:template match="Row/*"> 
    <td><xsl:apply-templates/></td> 
</xsl:template> 

当前HTML输出:

<THEAD> 
    <TR> 
     <TH>Date</TH><TH>Time</TH> 
    </TR> 
    </THEAD> 
    <TR> 
     <TD>DATA1</TD><TD>DATA2</TD> 
    </TR> 
    <TR> 
     <TD>DATA1</TD><TD>DATA2</TD> 
    </TR> 

如何我TBODY包装数据行?谢谢!

+1

因为只匹配“Columns”的模板具有更高的默认优先级(正在存在),所以匹配'“Columns | Row”'的模板将永远不会应用到'“Columns”更具体)。所以你不妨将'match ='Columns | Row''更改为'match =“Row”',以减少阅读时的混淆。 – LarsH 2012-01-12 18:10:26

回答

3

最简单的解决方案可能是下面的模板添加到您的样式表:

<xsl:template match="Rowset"> 
    <xsl:apply-templates select="Columns"/> 
    <tbody> 
     <xsl:apply-templates select="Row"/> 
    </tbody> 
</xsl:template> 

完整样式表(与其他几个小的改动):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/"> 
     <HTML> 
      <BODY> 
       <TABLE> 
        <xsl:apply-templates/> 
       </TABLE> 
      </BODY> 
     </HTML> 
    </xsl:template> 
    <xsl:template match="Rowset"> 
     <xsl:apply-templates select="Columns"/> 
     <tbody> 
      <xsl:apply-templates select="Row"/> 
     </tbody> 
    </xsl:template> 
    <xsl:template match="Columns"> 
     <thead><tr><xsl:apply-templates/></tr></thead> 
    </xsl:template> 
    <xsl:template match="Columns/*"> 
     <th><xsl:apply-templates select="@Description"/></th> 
    </xsl:template> 
    <xsl:template match="Row"> 
     <tr><xsl:apply-templates/></tr> 
    </xsl:template> 
    <xsl:template match="Row/*"> 
     <td><xsl:apply-templates/></td> 
    </xsl:template> 
</xsl:stylesheet> 
+0

谢谢,这是完美的! – user1130511 2012-01-17 01:22:28

3

您可以限制(选择)由apply-templates应用哪些节点。 我会使用这样的:

<xsl:strip-space elements="*"/> 
<xsl:template match="/"> 
    <HTML> 
    <BODY> 
     <TABLE> 
     <THEAD> 
      <xsl:apply-templates select="Columns"/> 
     </THEAD> 
     <TBODY> 
      <xsl:apply-templates select="Row"/> 
     </TBODY> 
     </TABLE> 
    </BODY> 
    </HTML> 
</xsl:template> 

<xsl:template match="Columns|Row"> 
    <TR><xsl:apply-templates/></TR> 
</xsl:template> 

<xsl:template match="Columns/*"> 
    <TH><xsl:value-of select="@Description"/></TH> 
</xsl:template> 

<xsl:template match="Row/*"> 
    <TD><xsl:apply-templates/></TD> 
</xsl:template>