English 中文(简体)
XSLT + 创建表格结构
原标题:XSLT + Creating table structure
  • 时间:2012-01-12 17:32:46
  •  标签:
  • xml
  • xslt

我愿建立一个表格结构,将头盔分到THEAD,并将数据分到TBODY:

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

www.un.org/Depts/DGACM/index_spanish.htm The following XSLT does separates the Header and body but I can tates how to Pack the tags between the data rows:

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

<Current hexachloroput:

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

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

最佳回答

最简单的解决办法可能是在你的风格上添加以下模板:

<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>
问题回答

You can restrict (select) what nodes shall be applyed by apply-templates. I d use something like this:

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




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签