XSLT 1.0- 使用 Muenchian 方法创建 table

XSLT 1.0- creating table with Muenchian Method

我在 XSLT 1.0 中分组时遇到了一些困难。

这是我的 XML 输入:

<?xml version="1.0" encoding="utf-8"?>
    <catalog>
        <collection>
            <musiccd>
                <title index="1" name="Empire Burlesque">
                    <person>
                        <artist index="1" name="Bob Dylan">
                        </artist>
                    </person>
                </title>
                <title index="2" name="Hide your heart">
                    <person>
                        <artist index="2" name="Bonnie Tyler">
                        </artist>
                    </person>
                </title>
                <title index="3" name="Greatest Hits">
                    <person>
                        <artist index="3" name="Dolly Parton">
                        </artist>
                    </person>
                </title>
            </musiccd>
            <moviedvd>
                <title index="A" name="Interstellar">
                    <person>
                        <actor index="A" name="Someone">
                        </actor>
                    </person>
                </title>
                <title index="B" name="Kungfupanda">
                    <person>
                        <actor index="B" name="Panda">
                        </actor>
                    </person>
                </title>
                <title index="C" name="Matrix">
                    <person>
                        <actor index="C" name="Neo">
                        </actor>
                    </person>
                </title>
            </moviedvd>
        </collection>
    </catalog>

这是我的 XSLT 代码:

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

<xsl:key name="musickey" match="catalog/collection/musiccd/title" use="@index"/>
<xsl:key name="moviekey" match="catalog/collection/moviedvd/title" use="@index"/>

<xsl:template match="/">
<html>
<body>
<table border="1">
    <tr bgcolor="#9acd32">
    <th class="underline" align="center" lang="en" >Column 1</th>
    <th class="underline" align="center" lang="en" >Column 2</th>
    <th class="underline" align="center" lang="en" >Column 3</th>
    <th class="underline" align="center" lang="en" >Column 4</th>
    </tr>

    <xsl:for-each select="catalog/collection/musiccd/title[generate-id(.) = generate-id(key('musickey',@index)[1])]">
        <tr>
        <td><xsl:value-of select="@index"/></td>
            <xsl:for-each select="key('musickey', @index)">
                <td><xsl:value-of select="@name"/></td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>

    <xsl:for-each select="catalog/collection/moviedvd/title[generate-id(.) = generate-id(key('moviekey',@index)[1])]">
        <tr>
        <td><xsl:value-of select="@index"/></td>
            <xsl:for-each select="key('moviekey', @index)">
                <td><xsl:value-of select="@name"/></td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

通过我的代码,我得到了这个 table: TABLE I GET

但我希望我的 table 看起来像这样: TABLE I WANT

有人知道如何修改我的代码以获得我想要的 table 吗?

我只能使用 XSLT 1.0。提前致谢。

我想重点是了解行结构,所以只需将其放在单独的模板中(例如 rows),如下面的 XSL 所示:

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

<xsl:key name="musickey" match="/catalog/collection/musiccd/title" use="@index"/>
<xsl:key name="moviekey" match="/catalog/collection/moviedvd/title" use="@index"/>

<xsl:template name="rows">
    <xsl:param name="pos"/>
    <tr>
        <td><xsl:value-of select="/catalog/collection/*[name()='musiccd']/title[$pos][generate-id(.) = generate-id(key('musickey',@index)[1])]/@index"/></td>
        <td><xsl:value-of select="/catalog/collection/*[name()='musiccd']/title[$pos][generate-id(.) = generate-id(key('musickey',@index)[1])]/@name"/></td>
        <td><xsl:value-of select="/catalog/collection/*[name()='moviedvd']/title[$pos][generate-id(.) = generate-id(key('moviekey',@index)[1])]/@index"/></td>
        <td><xsl:value-of select="/catalog/collection/*[name()='moviedvd']/title[$pos][generate-id(.) = generate-id(key('moviekey',@index)[1])]/@name"/></td>
    </tr>
</xsl:template>

<xsl:template match="/">
<html>
<body>
<table border="1">
    <tr bgcolor="#9acd32">
    <th class="underline" align="center" lang="en" >Column 1</th>
    <th class="underline" align="center" lang="en" >Column 2</th>
    <th class="underline" align="center" lang="en" >Column 3</th>
    <th class="underline" align="center" lang="en" >Column 4</th>
    </tr>
    <!--put first block in loop as we know that amount of titles in both blocks are same-->
    <xsl:for-each select="/catalog/collection/*[1]">
        <xsl:for-each select="title">
            <xsl:call-template name="rows">
                <xsl:with-param name="pos" select="position()"/>
            </xsl:call-template>
       </xsl:for-each>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

结果你会得到:

<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th class="underline" align="center" lang="en">Column 1</th><th class="underline" align="center" lang="en">Column 2</th><th class="underline" align="center" lang="en">Column 3</th><th class="underline" align="center" lang="en">Column 4</th>
</tr>
<tr>
<td>1</td><td>Empire Burlesque</td><td>A</td><td>Interstellar</td>
</tr>
<tr>
<td>2</td><td>Hide your heart</td><td>B</td><td>Kungfupanda</td>
</tr>
<tr>
<td>3</td><td>Greatest Hits</td><td>C</td><td>Matrix</td>
</tr>
</table>
</body>
</html>

在浏览器中: