XSLT WordML表,其动态列基于节点数

时间:2012-11-01 19:08:48

标签: xml xslt foreach xmltable wordml

我有许多xml文件,我正在尝试创建一个WordML表。每个节点列表中都有许多节点,我需要为每个节点列创建一个列,最多15列。但是,如果小于15,则需要对齐列。

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

名为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循环。我很感激任何建议/指导。

1 个答案:

答案 0 :(得分:0)

Efrain提供的链接是正确的方向,但我认为它可以更简单。

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

对标题和行使用相同的变量。