XSLT将XML层次结构展平为HTML表

我有一些像这样的分层XML

<node text="a" value="1">
  <node text="gga" value="5">  
    <node text="dh" value="9">
      <node text="tyfg" value="4">  
      </node>  
    </node>  
  </node>  
  <node text="dfhgf" value="7">  
    <node text="fdsg" value="2">  
    </node>  
  </node>  
</node>

元素的名称一直到下(“节点”)都是相同的,并且层次的深度是事先未知的–在上面的示例中,最深的叶子是四层,但可以是任何深度.

我需要做的是获取此XML并将其展平为HTML表.表中的列数应等于最深元素的深度,再加上每个元素的value属性的列. “值”应该出现在表的最右列,因此输出行不能有参差不齐的边缘.不论其处于哪个级别,每个节点都应有一行.上面的示例应转换为:

<table>
  <tr>
    <td>a</td>
    <td></td>
    <td></td>
    <td></td>
    <td>1</td>
  </tr>
  <tr>
    <td>a</td>
    <td>gga</td>
    <td></td>
    <td></td>
    <td>5</td>
  </tr>
  <tr>
    <td>a</td>
    <td>gga</td>
    <td>dh</td>
    <td></td>
    <td>9</td>
  </tr>
  <tr>
    <td>a</td>
    <td>gga</td>
    <td>dh</td>
    <td>tyfg</td>
    <td>4</td>
  </tr>
  <tr>
    <td>a</td>
    <td>dfhgf</td>
    <td></td>
    <td></td>
    <td>7</td>
  </tr>
  <tr>
    <td>a</td>
    <td>dfhgf</td>
    <td>fdsg</td>
    <td></td>
    <td>2</td>
  </tr>
</table>

有人有一些聪明的XSLT可以实现吗?

最佳答案
它不是您所需要的(因为它留下了一个锯齿状的表),但是仍然可以在html中使用

<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <table>
                <xsl:apply-templates select="//node" mode="row" />
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="node" mode="row">
    <tr>
        <xsl:apply-templates select="ancestor-or-self::node" mode="popcell"/>   
        <xsl:apply-templates select="node[1]" mode="emptycell"/>
    </tr>
</xsl:template>

<xsl:template match="node" mode="popcell">
    <td><xsl:value-of select="@text"/></td>
</xsl:template>

<xsl:template match="node" mode="emptycell">
    <td></td>
    <xsl:apply-templates select="node[1]" mode="emptycell"/>
</xsl:template>

版本2:好吧,我对:P的自满程度不那么满意,但是以下内容消除了锯齿:

<xsl:variable name="depth">
    <xsl:for-each select="//node">
        <xsl:sort select="count(ancestor::node)" data-type="number" order="descending"/>
        <xsl:if test="position()=1">
            <xsl:value-of select="count(ancestor::node)+1"/>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <table>
                <xsl:apply-templates select="//node" mode="row" />
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="node" mode="row">
    <tr>
        <xsl:apply-templates select="ancestor-or-self::node" mode="popcell"/>
        <xsl:call-template name="emptycells">
            <xsl:with-param name="n" select="($depth)-count(ancestor-or-self::node)"/>
        </xsl:call-template>
        <td><xsl:value-of select="@value"/></td>
    </tr>
</xsl:template>

<xsl:template match="node" mode="popcell">
    <td><xsl:value-of select="@text"/></td>
</xsl:template>

<xsl:template name="emptycells">
    <xsl:param name="n" />
    <xsl:if test="$n &gt; 0">
        <td></td>   
        <xsl:call-template name="emptycells">
            <xsl:with-param name="n" select="($n)-1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

相关文章

HTML代码中要想改变字体颜色,常常需要使用CSS样式表。CSS是...
HTML代码如何让字体盖住图片呢?需要使用CSS的position属性及...
HTML代码字体设置 在HTML中,我们可以使用标签来设置网页中的...
在网页设计中,HTML代码的字体和字号选择是非常重要的一个环...
HTML(Hypertext Markup Language,超文本标记语言)是一种用...
外链是指在一个网页中添加一个指向其他网站的链接,用户可以...