本文介绍了XSLT + 创建表结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表结构,将标题行按 THEAD 分隔,数据行按 TBODY 分隔:

I would like to create a table structure that separates the header row by THEAD and the data rows by TBODY:

输入 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 包装数据行?谢谢!

How can I wrap the data rows with TBODY? Thanks!

推荐答案

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

The simplest solution is probably to add the following template to your stylesheet:

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

完整的样式表(还有一些其他的小改动):

Complete stylesheet (with a couple other minor changes):

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

这篇关于XSLT + 创建表结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:33