如何在多个级别进行分组?

时间:2011-04-11 14:00:49

标签: xslt

我有一个XML文件:

<document>
    <line id="0">
        <field id="2">X111</field>
        <field id="3">1</field>
        <field id="4">222222222222</field>
    </line>
    <line id="1">
        <field id="2">X111</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
    <line id="2">
        <field id="2">X222</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
    <line id="3">
        <field id="2">X222</field>
        <field id="3">1></field>
        <field id="4">111111111111</field>
    </line>
    <line id="4">
        <field id="2">X333</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
</document>

从这个xml文件中我应该对field2进行分组(在该field4之后),问题将不是如何对字段2进行分组并得到三个文件,但如果它们是相同的,如何对field4进行分组?

输出:

<document>
<Result>
    <Header>
        <Field2>X111</Field2>
    </Header>
    <Line>
        <Position>1</Position>
        <Field4>222222222222</Field4>
        <Sum>1<Sum>
        <Position>2</Position>
        <Field4>111111111111</Field4>
        <Sum>1<Sum>
    </Line>
</Result>
<Result>
    <Header>
        <Field2>X222</Field2>
    </Header>
    <Line>
        <Position>1</Position>
        <Field4>111111111111</Field4>
        <Sum>2<Sum>
    </Line>
</Result>
<Result>
    <Header>
        <Field2>X333</Field2>
    </Header>
    <Line>
        <Position>1</Position>
        <Field4>111111111111</Field4>
        <Sum>1</Sum>
    </Line>
</Result>
</document>

我被困在分组线上,我不知道如何对id = 4的相同和不同的字段进行分组。

我的程序看起来:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:key name="kLine" match="line" use="string(field[@id=2])"/>
    <xsl:key name="bLine" match="line" use="field[@id=4]"/>
    <xsl:template match="document">
        <document>
            <xsl:apply-templates select="line[count( . | key('kLine', string(field[@id='2']))[1]) = 1]"/>
        </document>
    </xsl:template>
    <xsl:template match="line">
        <xsl:variable name="field2" select="field[@id='2']"/>
        <result>
            <Header>
                <xsl:value-of select="field[@id='2']"/>
            </Header>
            <Line>
            <xsl:for-each select="//line[field[@id='2']=$field2]">
                <Position>
                    <xsl:value-of select="position()"/>
                </Position>
                <Field4><xsl:value-of select="field[@id='4']"/></Field4>
                <Sum><xsl:value-of select="sum(key('bLine', field[@id='4'])/field[@id='3'])"/></Sum>
            </xsl:for-each>
            </Line>
        </result>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

你问的是如何在多个级别进行分组。以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="byField2" match="line" use="field[@id='2']" />
    <xsl:key name="byField2AndField4" match="line"
             use="concat(field[@id='2'], '|', field[@id='4'])" />
    <xsl:template match="/">
        <document><xsl:apply-templates /></document>
    </xsl:template>
    <xsl:template
        match="line[generate-id() = 
                    generate-id(key('byField2', field[@id='2'])[1])]">
        <Result>
            <Header>
                <Field2><xsl:value-of select="field[@id='2']" /></Field2>
            </Header>
            <Line>
                <xsl:apply-templates
                    select="key('byField2', field[@id='2'])
                         [generate-id() =
                          generate-id(key('byField2AndField4',
                             concat(field[@id='2'], '|', field[@id='4']))[1])]"
                    mode="field4" />
            </Line>
        </Result>
    </xsl:template>
    <xsl:template match="line" mode="field4">
        <Position><xsl:value-of select="position()" /></Position>
        <Field4><xsl:value-of select="field[@id='4']" /></Field4>
        <Sum>
            <xsl:value-of
              select="sum(key('byField2AndField4',
                 concat(field[@id='2'], '|', field[@id='4']))/
                     field[@id='3'])" />
        </Sum>
    </xsl:template>
    <xsl:template match="line" />
</xsl:stylesheet>

在此输入上:

<document>
    <line id="0">
        <field id="2">X111</field>
        <field id="3">1</field>
        <field id="4">222222222222</field>
    </line>
    <line id="1">
        <field id="2">X111</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
    <line id="2">
        <field id="2">X222</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
    <line id="3">
        <field id="2">X222</field>
        <field id="3">1></field>
        <field id="4">111111111111</field>
    </line>
    <line id="4">
        <field id="2">X333</field>
        <field id="3">1</field>
        <field id="4">111111111111</field>
    </line>
</document>

产生所需的结果:

<document>
    <Result>
        <Header>
            <Field2>X111</Field2>
        </Header>
        <Line>
            <Position>1</Position>
            <Field4>222222222222</Field4>
            <Sum>1</Sum>
            <Position>2</Position>
            <Field4>111111111111</Field4>
            <Sum>1</Sum>
        </Line>
    </Result>
    <Result>
        <Header>
            <Field2>X222</Field2>
        </Header>
        <Line>
            <Position>1</Position>
            <Field4>111111111111</Field4>
            <Sum>2</Sum>
        </Line>
    </Result>
    <Result>
        <Header>
            <Field2>X333</Field2>
        </Header>
        <Line>
            <Position>1</Position>
            <Field4>111111111111</Field4>
            <Sum>1</Sum>
        </Line>
    </Result>
</document>

我们使用两把钥匙。第一组仅由字段2组成。第二组按字段2和4连接。