Xml 需要使用“确定cols编号”;运输署";表中的元素

Xml 需要使用“确定cols编号”;运输署";表中的元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我想通过在“tr”元素内部使用“td”元素的计数来添加元素的属性值 我的输入xml: <table> <tbody> <tr> <td> <p>Type</p> </td> <td> <p>Risk</p> </td> </tr> <tr> <td> <p>Fundic</p> </td> &l

我想通过在“tr”元素内部使用“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 I用作:

   <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”的计数

请建议我对此进行编码。提前感谢

使用此

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