2012-11-01 56 views
3

我有一些從我試圖創建一個WordML中表XML文件。每個節點列表中都有很多節點,我需要爲每個節點創建一個列,最多可以有15列。但是,如果少於15個,則列需要被證明是合理的。XSLT的WordML表基於節點的數量動態列

模板稱爲節點列表:

<xsl:template match="NodeList"> 
<xsl:for-each select="NodeRef"> 
<xsl:sort data-type="number" select="@Position" order="ascending"/> 
<xsl:sort data-type="text" select="@Name" order="ascending"/> 
<xsl:variable name="documentName" select="concat(@Id, '_Nodes.xml')"/> 
<xsl:apply-templates mode="SimpleNode" select="document($documentName)/Node"/> 
</xsl:for-each> 
</xsl:template> 

模板稱爲SimpleNode的:

<xsl:template mode="SimpleNode" match="Node"> 
<!-- Output the Node Table - as template within for-each, 
will output table many times --> 
<xsl:call-template name="SimpleNodeTable"/> 
</xsl:template> 

SimpleNodeTable範本(我卡):

<xsl:template name="SimpleNodeTable"> 
<w:tbl> 
    <w:tblPr> 
    <w:tblStyle w:val="ReportTable1"/> 
    <w:tblW w:type="dxa"> 
     <xsl:attribute name="w:w"> 
     <xsl:value-of select="$landscapeBodyWidth"/> 
     </xsl:attribute> 
    </w:tblW> 
    <w:tblLayout w:type="Fixed"/> 
    </w:tblPr> 
    <w:tblGrid> 
    <w:gridCol> 
     <xsl:attribute name="w:w"> 
     <xsl:value-of select="$nodeNameWidth"/> 
     </xsl:attribute> 
    </w:gridCol> 
    <w:gridCol> 
     <xsl:attribute name="w:w"> 
     <xsl:value-of select="$landscapeBodyWidth - $nodeNameWidth"/> 
     </xsl:attribute> 
    </w:gridCol> 
    </w:tblGrid> 
    <w:tr> 
    <!-- first row --> 
    <w:tc> 
     <!-- First cell is blank so vertically merge --> 
     <w:vmerge w:val="restart"/> 
     <w:p/> 
    </w:tc> 
    <!-- HOW TO DO A FOR EACH LOOP TO ADD 1 COLUMN PER NODEREF?? --> 
    <xsl:for-each select="Node"> 
    <w:tc> 
     <w:p> 
     <!-- Second cell contains the name of the node (from xml file) --> 
     <w:t> 
      <xsl:value-of select="NodeName"/> 
     </w:t> 
     </w:p> 
    </w:tc> 
    </xsl:for-each> 
    </w:tr> 
</w:tbl> 
</xsl:template> 

也許我在這臨近一個完全錯誤的方式,但我已經結束了混淆自己試圖獲得1桌,然後開始for-each循環。我會很感激任何建議/指導。

+0

應該不是你的女:tblGrid結構包含儘可能多寬:gridCol項目爲節點列表節點的源XML文件的最大數量? – collapsar

+0

我不知道我的理解..你的問題是,你要轉表,對不對?如果是這樣,看看這個:http://stackoverflow.com/questions/1171376/matrix-transposition-in-xslt – Efrain

回答

0

由埃弗拉提供的鏈接是一個正確的方向,但我認爲它可以更簡單。

<xsl:variable name="max-col"> 
    <xsl:choose> 
     <xsl:when test="count(Node) &gt; 15">16</xsl:when> 
     <xsl:otherwise><xsl:value-of select="count(Node) + 1"/></xsl:otherwise> 
    </xsl:choose> 
</xsl:variable> 

<!-- Then take only the first 'max-col' Nodes --> 
<xsl:for-each select="Node[position() &lt; $max-col]"> 
    <!-- ..... your code ....... --> 
</xsl:for-each> 

對標題和行使用相同的變量。