3

I have a number of xml files from which I'm trying to create a WordML table. There are a number of nodes in each nodelist, and I need to create a column for each one, up to a maximum of 15 columns. However, if there are less than 15, the columns need to be justified.

Template called NodeList:

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

Template called 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 template (where I'm stuck):

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

Perhaps I'm approaching this in a completely wrong way but I've ended up confusing myself with trying to get 1 table and then starting the for-each loop. I'd be grateful for any advice/guidance.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Shouldn't your w:tblGrid structure contain as many w:gridCol items as the Maximum number of Nodelist nodes in the Source XML Files? – collapsar Feb 22 '13 at 07:10
  • I'm not sure I understand.. your problem is that you want to transpose the table, right? if so, have a look at this: http://stackoverflow.com/questions/1171376/matrix-transposition-in-xslt – Efrain Jul 12 '13 at 13:32

1 Answers1

0

The link provided by Efrain is the right direction but I think it can be even simpler.

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

Use the same variable for the header and for the rows.

OmegaZiv
  • 256
  • 2
  • 14