如何使用xslt进行解析以获得具有合并头的这种复杂表

时间:2014-06-09 08:16:34

标签: xslt

我有一些xslt脚本来解析xml输入并返回html文件。到目前为止它工作正常。但是现在我需要解析xml输入以获得具有合并头的这个复杂表。我想要的输出如下。

|       V1         ||          V2        |
|  version diff    ||   version  diff    |                               
==========================================
|                  ||                    |
|     CONTENT      ||       CONTENT      |
|                  ||                    |

由于Category1,Category2,Col1,Col2,Col3,Col4都是表头。但是合并了Cateogry1,Cateogry2以使表格更加清晰。

我这样的xml输入,

<?xml version="1.0" encoding="utf-8"?>
<Index>
  <entry name="Entry1">
    <entry name="V1.baseversion">1.0.0.77</entry>
    <entry name="V1.sizedifferencescount">0</entry>
    <entry name="V2.sizedifferencescount">0</entry>
    <entry name="V2.baseversion">1.0.0.75</entry>
  </entry>
  <entry name="Entry2">
    <entry name="V1.baseversion">3.0.0.12</entry>
    <entry name="V1.sizedifferencescount">0</entry>
    <entry name="V2.sizedifferencescount">0</entry>
    <entry name="V2.baseversion">3.0.0.13</entry>
  </entry>
 </Index>

以下是我到目前为止尝试过的xslt,我打算用它来解析标题。

 <xsl:when test="position()=1">
    <xsl:for-each  select="*" > 
        <xsl:sort select="@name"/>
        <xsl:if test=" ( (@name='V1.baseversion') or (@name='V1.sizedifferencescount') or (@name='V2.baseversion') or (@name='V2.sizedifferencescount'))">
            <tr>
                <th>
                    <xsl:attribute name="colspan">
                        <xsl:value-of select="2" />
                    </xsl:attribute>
                    <!--need to print Version 1 here-->
                </th>
                <th>
                    <xsl:attribute name="colspan">
                        <xsl:value-of select="2" />
                    </xsl:attribute>
                    <!--need to print Version 2 here-->
                </th>
            </tr>
            <tr>
                <th>
                    <!--need to print V1.baseversion here-->
                </th>
                <th>
                    <!--need to print V1.sizedifferencescount here-->
                </th>       
                <th>
                    <!--need to print V2.baseversion here-->
                </th>
                <th>
                    <!--need to print V2.sizedifferencescount here-->
                </th>                                       
            </tr>                                 
        </xsl:if>
    </xsl:for-each>
</xsl:when>

在输出html中,xml中的每个条目都是表中的一行。输出html应该是这样的。

<table>
    <tr>
        <th colspan=2>V1</th>
        <th colspan=2>V2</th>

    </tr>
    <tr>
        <th >Version</th>
        <th >Diff</th>
        <th >Version</th>
        <th >Diff</th>
    </tr>
    <tr>
        <td>XXXX</td><!--Display value for Entry1.V1.baseversion-->
        <td>XXXX</td><!--Display value for Entry1.V1.sizedifferencescount-->
        <td>XXXX</td><!--Display value for Entry1.V2.baseversion-->
        <td>XXXX</td><!--Display value for Entry1.V2.sizedifferencescount-->
    </tr>
    <tr>
        <td>XXXX</td><!--Display value for Entry2.V1.baseversion-->
        <td>XXXX</td><!--Display value for Entry2.V1.sizedifferencescount-->
        <td>XXXX</td><!--Display value for Entry2.V2.baseversion-->
        <td>XXXX</td><!--Display value for Entry2.V2.sizedifferencescount-->
    </tr>
</table>

感谢您提供的信息和帮助。

亚历

1 个答案:

答案 0 :(得分:0)

我仍然认为您的描述中缺少某些内容,但为了推动这一进程,请大致如何:

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <table>
        <tr>
            <th colspan="2">V1</th>
            <th colspan="2">V2</th>
        </tr>
        <tr>
            <th>Version</th>
            <th>Diff</th>
            <th>Version</th>
            <th>Diff</th>
        </tr>
        <xsl:for-each select="Index/entry">
            <tr>
                <td><xsl:value-of select="entry[@name='V1.baseversion']"/></td>
                <td><xsl:value-of select="entry[@name='V1.sizedifferencescount']"/></td>
                <td><xsl:value-of select="entry[@name='V2.baseversion']"/></td>
                <td><xsl:value-of select="entry[@name='V2.sizedifferencescount']"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>