Q:

Need to fix the cols number by using the "td" element in the table

I want to add the attribute valve for  element by using the count of "td" element using inside the "tr" element.
My Input 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 I used as:
   <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>

Output I'm getting as:
 <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>

Expected Output be like:
 <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>

I need the cols value by using the count of "td" inside the "tr". If the single "td" means cols="1" and its depends upon the count of multiple "td" using inside the "tr"
Please suggest me coding for this. Thanks in advance

A:

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

