我想讓我的頭繞過XSL遞歸。我需要使用帶有單元標籤的XSLT構建基於XML的HTML表格,其中位置映射到名爲格式爲「row.column」的名稱的字符串屬性。基於變量的XSL遞歸遍歷
我想遞歸地從最後一個單元格開始計數回到第一個單元格(0.0)並開始構建表格。原因是有些單元可能跨越幾個單元位置。因此,爲了在HTML中使用,我不能只在for-each循環中處理單元格。最後一個單元格由表格屬性標識; BodyRowCount和ColumnCount。
的XML看起來像這樣
<Table HeaderRowCount="0" FooterRowCount="0" BodyRowCount="4" ColumnCount="2" >
\t <Cell Self="ucd8iceei0" Name="0:0" RowSpan="1" ColumnSpan="1">
\t \t <Content>1</Content>
\t </Cell>
\t <Cell Self="ucd8iceei1" Name="1:0" RowSpan="1" ColumnSpan="1">
\t \t <Content>2</Content>
\t </Cell>
\t <Cell Self="ucd8iceei2" Name="2:0" RowSpan="1" ColumnSpan="2>
\t \t <Content>3</Content>
\t </Cell>
\t <Cell Self="ucd8iceei3" Name="3:0" RowSpan="1" ColumnSpan="1">
\t \t <Content>4</Content>
\t </Cell>
\t <Cell Self="ucd8iceei4" Name="0:1" RowSpan="1" ColumnSpan="1">
\t \t <Content>5</Content>
\t </Cell>
\t <Cell Self="ucd8iceei6" Name="2:1" RowSpan="1" ColumnSpan="1">
\t \t <Content>7</Content>
\t </Cell>
\t <Cell Self="ucd8iceei7" Name="3:1" RowSpan="1" ColumnSpan="1">
\t \t <Content>8</Content>
\t </Cell>
</Table>
結果應該是這樣的
<Table>
\t <tr>
\t \t <td>1</td> \t <td>5</td>
\t </tr>
\t <tr>
\t \t <td> colspan="2">2</td>
\t </tr>
\t <tr>
\t \t <td>3</td> <td>7</td>
\t </tr>
\t <tr>
\t \t <td>4</td> \t <td>8</td>
\t </tr>
</Table>
僞形式的XLS在下面。我嘗試在名爲4.2的Cell中傳遞參數以開始這種情況,但似乎是不合法的,不能通過基於屬性的參數,但是這肯定是可能的?
<xsl:template match="Table"> \t
\t <table>
\t \t <xsl:apply-templates select="Cell[@Name = concat(@BodyRowCount,':',ColumnCount)']"/>
\t </table>
</xsl:template>
<xsl:template name="Cell[@name='0.0">
Found first cell, start first row in table
<tr><td colspan=<xsl:value-of select=(concat(3",@ColumnSpan,3")) rowspan=<xsl:value-of select=(concat(3",@rowSpan,3"))> Row content here </td></tr>
</xsl:template>
<xsl:template name="Cell[@name='0.*">
\t Call template with 'ROW-1, COLUMN back to max
\t <xsl:apply-templates select="Cell[@Name = 'ROW-1:MAXCOLUMN']">
\t new row goes here
\t <tr><td colspan=<xsl:value-of select=(concat(3",@ColumnSpan,3")) rowspan=<xsl:value-of select=(concat(3",@rowSpan,3"))> Row content here </td></tr>
</xsl:template>
<xsl:template name="Cell[@name='*.*">
\t Call template with one column before
\t <xsl:apply-templates select="Cell[@Name = SAME ROW:COLUMN-1']">
\t <td colspan=<xsl:value-of select=(concat(3",@ColumnSpan,3")) rowspan=<xsl:value-of select=(concat(3",@rowSpan,3"))> Row content here </td>
</xsl:template>
我尋找關於如何啓動的遞歸循環中4.2回到0.0,通過遍歷所有的位置,如果一個存在插入單元格的值建表一些幫助?
您在使用XSLT 1.0和XSLT 2.0? –
嗨我使用V2,但如果我更改爲V1,那麼Andreas提供的示例沒有錯誤地工作。所以看起來版本的行爲有所不同。 – Christian
如果您使用XSLT 2.0,那麼您可以使用內置的分組功能。 - 你能解釋一下什麼決定了這個colspan?在你的例子中,每個單元格都有RowSpan和ColumnSpan屬性 - 但是你的輸出具有不同的值。這是爲什麼? –