XSLT 1.0- 使用 Muenchian 方法创建表

Posted

技术标签:

【中文标题】XSLT 1.0- 使用 Muenchian 方法创建表【英文标题】:XSLT 1.0- creating table with Muenchian Method 【发布时间】:2018-06-24 04:23:50 【问题描述】:

我在 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 I GET

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

有人知道如何修改我的代码来获取我想要的表格吗?

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

【问题讨论】:

【参考方案1】:

我想主要是要知道行结构,所以把它放在单独的模板中(例如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>

在浏览器中:

【讨论】:

以上是关于XSLT 1.0- 使用 Muenchian 方法创建表的主要内容,如果未能解决你的问题,请参考以下文章

Muenchian 分组 XSLT 1.0 多重分组

我无法理解 XSLT 1.0 Muenchian 分组

使用 XSLT 对 HTML 输出进行分组(muenchian 分组?)

使用 XSLT muenchian-grouping 进行嵌套分组

XSLT 和 Muenchian 分组, 多级样本

使用 XSLT 对简单 XML 应用 Muenchian 分组