首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT在Mule中按特定特征聚合

问使用XSLT在Mule中按特定特征聚合
EN

Stack Overflow用户
提问于 2013-06-21 04:27:44
回答 1查看 143关注 0票数 0

我正在使用Mule从数据库中提取信息,并将其格式化为-

代码语言:javascript
复制
<item>
    <id>1</id>
    <group_id>1</group_id>
    <color>blue</color>
    <city>Chicago</city>
</item>

有几条这样的记录,主键是id,所以我有几条这样的记录,每条记录都在不同的MuleMessage中。我想按group_id分组,这样我的每个MuleMessages看起来就像这样-

代码语言:javascript
复制
<item>
    <group_id>1</group_id>
    <id>1</id>
    <id>2</id>
    <id>3</id>
</item>

我知道我必须按聚合器对消息进行分组,但我不确定如何将group_id作为聚合属性。我还需要使用XSLT转换器吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-21 14:23:21

需要此输入文件

代码语言:javascript
复制
<items>
<item>
    <id>1</id>
    <group_id>1</group_id>
    <color>blue</color>
    <city>Chicago</city>
</item>
<item>
    <id>2</id>
    <group_id>1</group_id>
    <color>red</color>
    <city>Chicago</city>
</item>
<item>
    <id>3</id>
    <group_id>1</group_id>
    <color>yellow</color>
    <city>Detroit</city>
</item>
<item>
    <id>4</id>
    <group_id>2</group_id>
    <color>cyan</color>
    <city>Washington</city>
</item>
<item>
    <id>5</id>
    <group_id>2</group_id>
    <color>gray</color>
    <city>Colorado</city>
</item>
</items>

这个xslt可以生成您需要的输出

代码语言:javascript
复制
<xsl:template match="/">
    <xsl:apply-templates select="items" />
</xsl:template>

<xsl:template match="items">
    <xsl:for-each-group select="item" group-by="group_id">
        <xsl:element name="item">
            <xsl:element name="group_id">
                <xsl:value-of select="current-grouping-key()" />
            </xsl:element>
                <xsl:for-each select="current-group()">
                    <xsl:element name="id">
                        <xsl:value-of select="id" />
                    </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:for-each-group>

</xsl:template>

​

输出

代码语言:javascript
复制
<item>
    <group_id>1</group_id>
    <id>1</id>
    <id>2</id>
    <id>3</id>
</item>
<item>
    <group_id>2</group_id>
    <id>4</id>
    <id>5</id>
</item>

编辑:如果你没有一个包含所有条目的输入文件,而是有多个包含单个条目的文件,你可以这样做:

代码语言:javascript
复制
<xsl:variable name="files" select="collection('file:///path/to/files/item*.xml')"/>

<xsl:template match="/">
    <xsl:element name="items">
        <xsl:for-each-group select="$files/item" group-by="group_id">
            <xsl:element name="item">
                <xsl:element name="group_id">
                    <xsl:value-of select="current-grouping-key()" />
                </xsl:element>
                    <xsl:for-each select="current-group()">
                        <xsl:element name="id">
                            <xsl:value-of select="id" />
                        </xsl:element>
                    </xsl:for-each>
            </xsl:element>
        </xsl:for-each-group>
    </xsl:element>
</xsl:template>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17223161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档