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

发布于 2024-10-31 17:26:03 字数 3614 浏览 0 评论 0原文

我有一个 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>

I have an XML file:

<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>

From this xml file I should group field2 (after that field4 ), question will be not to how to group field 2 and get three documents but how to group field4 if they are the same?

Output:

<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>

I'm stuck in grouping lines, I didn't know how to group the same and different fields which id= 4.

My program looks:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2024-11-07 17:26:03

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

<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 的串联分组。

You're asking how to group at multiple levels. The following stylesheet:

<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>

On this input:

<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>

Produces the desired result:

<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>

We use two keys. The first groups only by field 2. The second groups by the concatenation of fields 2 and 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文