通过XSLT处理html时需要插入节点

通过XSLT处理html时需要插入节点,xslt,Xslt,我正在使用XSLT1.0将html转换为xml。这是我的意见 <table> <tr id=1> <td rowspan=2> <td> </tr> <tr id=2> <td> </tr> </table> 这是html中的正常行范围代码。 如果我在td(id=1)中有属性rowspan,我必须在tr(id=2)中已经存在的td前面创建一个新元素td。连续t

我正在使用XSLT1.0将html转换为xml。这是我的意见

<table>
<tr id=1>
   <td rowspan=2>
   <td>
</tr>
<tr id=2>
   <td>   
</tr>
</table>

这是html中的正常行范围代码。 如果我在td(id=1)中有属性rowspan,我必须在tr(id=2)中已经存在的td前面创建一个新元素td。连续tr中新元素td的数量取决于rowspan值

如何在xsl模板中(或xslt转换后的任何后期处理中)执行此操作..请帮助我..提前感谢

我会说得更清楚

这张桌子是3*3。考虑每个行中的第一列合并,所以我的输入HTML看起来像这个< /P>
<table>
<tr id="1">
    <td  rowspan="3">
         </p>
    </td>
   <td>
      </p>
   </td>
   <td>
      </p>
   </td>
</tr>
<tr id="2">
    <td>
         </p>
   </td>
   <td>
         </p>
   </td>
</tr>
<tr id="3">
   <td> 
         </p>
   </td>
   <td>
         </p>
   </td>
</tr>
</table>

id=2&3的tr有两个td,这意味着第一列与第一个tr-->td合并,后者的rowspan值为3(即合并了包括当前行单元格在内的3行)。我的结果应该是

<w:tbl> <!-- represents table -->
<w:tr> <!-- represents table tr-->
  <w:tc>   <!-- represents table td-->
     <w:tcPr>  <!-- represents td style-->
         <w:vMerge w:val="restart"/> <!-- added due to having rowspan attribute-->
      </w:tcPr>
       <w:p/>
  </w:tc>
  <w:tc>
       <w:p/>
  </w:tc>
  <w:tc>
       <w:p/>
  </w:tc>
</w:tr>
<w:tr>
    <w:tc>  <!-- have to add this node in addition-->
          <w:tcPr>  
              <w:vMerge/>
          </w:tcPr>           
          <w:p/>
    </w:tc>
    <w:tc> 
         <w:p/>
    </w:tc>
    <w:tc>
         <w:p/>
    </w:tc>
<w:tr>
<w:tr>
    <w:tc>  <!-- have to add this node in addition-->
          <w:tcPr>  
              <w:vMerge/>
          </w:tcPr>           
          <w:p/>
    </w:tc>
    <w:tc>
          <w:p/>
     </w:tc>
     <w:tc>
          <w:p/>
     </w:tc>
<w:tr>
</w:tbl>

更新:我最新的方法是根据第一行的单元格数量及其
行span
值来计算所需的单元格总数。因此,这个输入:

<table>
<tr id="1">
   <td rowspan="3"/>
   <td rowspan="2"/>
   <td/>
</tr>
<tr id="2">
   <td/>   
</tr>
<tr id="3">
   <td rowspan="2"/>
   <td/>   
</tr>
</table>

我希望有人能提供一个更短、更优雅的解决方案。

@vignesh-是不是
行span
只在第一行?或者每一行只需查看其前一行的
rowspan
值即可?您是否可以为上述输入发布所需的输出?仅供参考,在XSLT 1.0中,输入需要是格式良好的XML。因此,我假设上面的输入将被修改,以便
标记被关闭,属性值被引用,等等。同样,也许在上面你说的“in td(id=1)”是指“in*tr*(id=1)”?或者您的示例代码中的id=1应该在td而不是tr上?您提出的问题是一个有趣的问题,一个通用(完整)的XSLT解决方案是具有挑战性的。不过我很好奇,这是否真的是你需要做的。为什么必须将新的td放入rowspan单元格下面的输出表中?@vignesh:正如@LarsH所建议的,表“规范化”是最复杂的算法之一。但是您没有提供所需的输出,使您的问题不清楚真正的目标。这并不是第一个有这样错误的问题……请注意,
@rowspan
是行轴上的表达式,而不是列轴上的表达式。@Alejandro-这一点很好。我确信科尔斯潘是有意的。我甚至没有注意到。
<table>
<tr id="1">
   <td rowspan="3"/>
   <td rowspan="2"/>
   <td/>
</tr>
<tr id="2">
   <td/><td/><td/><td/><td/><td/>   
</tr>
<tr id="3">
   <td/><td/><td/><td rowspan="2"/>
   <td/>   
</tr>
</table>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="rowspan">
        <xsl:call-template name="sum">
            <xsl:with-param name="nodes" select="/table/tr[1]/td" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="tr[position() &gt; 1]/td[1]">
        <xsl:variable name="currcount">
            <xsl:call-template name="sum">
                <xsl:with-param name="nodes" select="../td" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="needed" select="$rowspan - $currcount" />
        <xsl:if test="$needed &gt; 0">
            <xsl:call-template name="loop">
                <xsl:with-param name="i">0</xsl:with-param>
                <xsl:with-param name="count">
                    <xsl:value-of select="$needed" />
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template name="loop">
        <xsl:param name="i" />
        <xsl:param name="count" />
        <td />
        <xsl:if test="$i &lt; $count - 1">
            <xsl:call-template name="loop">
                <xsl:with-param name="i">
                    <xsl:value-of select="$i + 1" />
                </xsl:with-param>
                <xsl:with-param name="count">
                    <xsl:value-of select="$count" />
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <!-- modified version of @Tomalak's code in question 1333558 -->
    <xsl:template name="sum">
        <xsl:param name="nodes" />
        <xsl:param name="sum" select="0" />
        <xsl:variable name="curr" select="$nodes[1]" />
        <!-- if we have a node, calculate & recurse -->
        <xsl:if test="$curr">
            <xsl:variable name="runningsum">
                <xsl:choose>
                    <xsl:when test="$curr[@rowspan]">
                        <xsl:value-of select="$sum + $curr/@rowspan" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$sum + 1" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:call-template name="sum">
                <xsl:with-param name="nodes" 
                                select="$nodes[position() &gt; 1]" />
                <xsl:with-param name="sum" select="$runningsum" />
            </xsl:call-template>
        </xsl:if>
        <!-- if we don't have a node (last recursive step), return sum -->
        <xsl:if test="not($curr)">
            <xsl:value-of select="$sum" />
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>