2017-02-27 74 views
1

我想通過在「tr」元素內使用「td」元素的計數來添加元素的屬性valve。需要通過使用表中的「td」元素來固定列號

我輸入XML:

<table> 
<tbody> 
<tr> 
<td> 
<p>Type</p> 
</td> 
<td> 
<p>Risk</p> 
</td> 
</tr> 
<tr> 
<td> 
<p>Fundic</p> 
</td> 
<td> 
<p>Low</p> 
</td> 
</tr> 
</tbody> 
</table> 

XSL我使用:

<xsl:template match="table"> 
     <table> 
      <xsl:if test="@title"> 
       <title><xsl:value-of select="@title"/></title> 
      </xsl:if> 
      <tgroup> 
     <xsl:apply-templates/> 
     </tgroup> 
     </table> 
    </xsl:template> 

    <xsl:template match="tbody"> 
     <tbody> 
     <xsl:apply-templates/> 
     </tbody>  
    </xsl:template> 

    <xsl:template match="th | tr"> 
     <row> 
     <xsl:apply-templates/> 
     </row>  
    </xsl:template> 

    <xsl:template match="td"> 
     <entry> 
     <xsl:if test="@align"> 
      <xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute> 
     </xsl:if> 
     <xsl:if test="@valign"> 
      <xsl:attribute name="valign"><xsl:value-of select="@valign"/></xsl:attribute> 
     </xsl:if> 
     <xsl:apply-templates/> 
     </entry>  
    </xsl:template> 

輸出我越來越爲:

<table> 
    <tgroup> 
     <tbody> 
      <row> 
      <entry> 
       <p>Type</p> 
      </entry> 
      <entry> 
       <p>Risk</p> 
      </entry> 
      </row> 
      <row> 
      <entry> 
       <p>Fundic</p> 
      </entry> 
      <entry> 
       <p>Low</p> 
      </entry> 
      </row> 
     </tbody> 
    </tgroup> 
</table> 

的預期輸出如:

<table> 
    <tgroup cols="2"> 
     <tbody> 
      <row> 
      <entry> 
       <p>Type</p> 
      </entry> 
      <entry> 
       <p>Risk</p> 
      </entry> 
      </row> 
      <row> 
      <entry> 
       <p>Fundic</p> 
      </entry> 
      <entry> 
       <p>Low</p> 
      </entry> 
      </row> 
     </tbody> 
    </tgroup> 
</table> 

我需要通過使用「tr」內的「td」的計數cols值。如果單個「td」表示cols =「1」,並且它取決於在「tr」內使用多個「td」的計數,請建議我爲此編碼。在此先感謝

回答

1

使用本

<xsl:template match="table"> 
    <table> 
     <xsl:if test="@title"> 
      <title><xsl:value-of select="@title"/></title> 
     </xsl:if> 
     <tgroup> 
      <xsl:attribute name="cols"> 
       <xsl:value-of select="count(descendant::tr[1]/td) + sum(descendant::tr[1]/td/@colspan)"/> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </tgroup> 
    </table> 
</xsl:template> 

<xsl:template match="tbody"> 
    <tbody> 
     <xsl:apply-templates/> 
    </tbody>  
</xsl:template> 

<xsl:template match="th | tr"> 
    <row> 
     <xsl:apply-templates/> 
    </row>  
</xsl:template> 

<xsl:template match="td"> 
    <entry> 
     <xsl:if test="@align"> 
      <xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute> 
     </xsl:if> 
     <xsl:if test="@valign"> 
      <xsl:attribute name="valign"><xsl:value-of select="@valign"/></xsl:attribute> 
     </xsl:if> 
     <p><xsl:apply-templates/></p> 
    </entry>  
</xsl:template> 
+0

再次感謝@Rupesh。它的工作正常 – User501

相關問題