2012-01-12 87 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>