使用XSLT 1.0 Muenchian Grouping创建HTML输出时,排序失败

时间:2014-08-14 16:11:19

标签: xslt xslt-grouping muenchian-grouping

我有以下XML:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="Test.xslt"?>
<test-results>
    <test-case name="TestCase1" description="Descriptiontext">
        <categories>
            <category name="Dimension linked to measure group" />
        </categories>
    </test-case>
    <test-case name="TestCase2" description="DescriptionText">
        <categories>
            <category name="Dimension linked to measure group" />
        </categories>
    </test-case>
    <test-case name="TestCase3" description="DescriptionText">
        <categories>
            <category name="Default parameters" />
        </categories>
    </test-case>
    <test-case name="TestCase4" description="DescriptionText">
        <categories>
            <category name="Default parameters" />
        </categories>
    </test-case>
    <test-case name="TestCase5" description="DescriptionText">
        <categories>
            <category name="Referential Integrity" />
        </categories>
        <reason>
            <message><![CDATA[Not testable, yet (v1.6.1)]]></message>
        </reason>
    </test-case>
    <test-case name="TestCase6" description="DescriptionText">
        <categories>
            <category name="Referential Integrity" />
        </categories>
        <reason>
            <message><![CDATA[Not testable, yet (v1.6.1)]]></message>
        </reason>
    </test-case>
</test-results>

使用以下XSLT,我尝试使用Muenchian分组按类别名称(升序)进行排序,并使用测试用例名称(升序)在每个类别中进行排序。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">
    <xsl:key name="cases-by-category" match="categories" use="category/@name" />
    <xsl:template match="test-case">
        <xsl:for-each select="categories[count(. | key('cases-by-category', category/@name)[1]) = 1]">
            <xsl:sort select="category/@name" />
            <xsl:value-of select="category/@name" /><br/>
            <xsl:for-each select="key('cases-by-category', category/@name)">
                <xsl:sort select="//test-case/@name" />
                <xsl:value-of select="//test-case/@name"/><br/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

然而,我得到的是:

Dimension linked to measure group
TestCase1
TestCase1
Default parameters
TestCase1
TestCase1
Referential Integrity
TestCase1
TestCase1

每个类别的测试用例数都是正确的,但是不会应用排序,并且始终使用第一个测试用例名称。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

鉴于<xsl:key name="cases-by-category" match="categories" use="category/@name" />,表达式key('cases-by-category', category/@name)为您提供了categories个元素的节点集,如果您想按父级排序,那么我认为您要使用{{1} }。

我也认为有

<xsl:sort select="../@name" />

看起来很奇怪,因为你会处理每个匹配的<xsl:template match="test-case"> <xsl:for-each select="categories[count(. | key('cases-by-category', category/@name)[1]) = 1]"> 元素的类别,它似乎更有可能你想要

test-case

代替。

以下是完整的示例:

<xsl:template match="test-results">
    <xsl:for-each select="test-case/categories[count(. | key('cases-by-category', category/@name)[1]) = 1]">

当我使用Saxon 6.5对您的输入进行操作时,我得到以下结果:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">

    <xsl:output indent="yes"/>

    <xsl:key name="cases-by-category" match="categories" use="category/@name" />

    <xsl:template match="/">
      <html>
        <body>
          <xsl:apply-templates/>
        </body>
      </html>
    </xsl:template>

    <xsl:template match="test-results">
      <xsl:for-each select="test-case/categories[count(. | key('cases-by-category', category/@name)[1]) = 1]">
            <xsl:sort select="category/@name" />
            <xsl:value-of select="category/@name" /><br/>
            <xsl:for-each select="key('cases-by-category', category/@name)">
                <xsl:sort select="../@name" />
                <xsl:value-of select="../@name"/><br/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>