0

Mule を使用してデータベースから情報を取得し、次のようにフォーマットしています -

<item>
    <id>1</id>
    <group_id>1</group_id>
    <color>blue</color>
    <city>Chicago</city>
</item>

このような id を主キーとするレコードがいくつかあるため、これらのレコードをいくつか、それぞれ別の MuleMessage に持っています。group_id でグループ化したいので、各 MuleMessages は次のようになります。

<item>
    <group_id>1</group_id>
    <id>1</id>
    <id>2</id>
    <id>3</id>
</item>

アグリゲーターによってメッセージをグループ化する必要があることはわかっていますが、group_id を集約属性として配置する方法がわかりません。XSLT トランスフォーマーも使用する必要がありますか?

4

1 に答える 1

1

この入力ファイルが必要です

<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 は、必要な出力を作成できます

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

出力

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

編集:すべてのアイテムを含む1つの入力ファイルではなく、単一のアイテムを含む複数のファイルがある場合、次のようにすることができます:

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

于 2013-06-21T06:23:21.020 に答える