Tuesday, 15 June 2010

xml - Aggregate by certain characteristic in Mule using XSLT -


I am using mule to draw information from the database and format it like this - < Pre> & lt; Items & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; Color & gt; Blue & lt; / Color & gt; & Lt; City & gt; Chicago & lt; / City & gt; & Lt; / Item & gt;

In this way there are many records in the form of primary keys with ID, so I have many of these records, each in a separate MuleMessage, I want to group by group_id, Selected messages look like this -

  & lt; Item & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; ID & gt; 3 & lt; / Id & gt; & Lt; / Item & gt;   

I know that I have to create a message group by an aggregator, but I'm not sure how to put group_id in the form of a combined attribute. Do I have to use XSLT Transformer too?

Expected this input file

  & lt; Items & gt; & Lt; Items & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; Color & gt; Blue & lt; / Color & gt; & Lt; City & gt; Chicago & lt; / City & gt; & Lt; / Item & gt; & Lt; Items & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; Color & gt; Red & lt; / Color & gt; & Lt; City & gt; Chicago & lt; / City & gt; & Lt; / Item & gt; & Lt; Items & gt; & Lt; ID & gt; 3 & lt; / Id & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; Color & gt; Yellow & lt; / Color & gt; & Lt; City & gt; Detroit & lt; / City & gt; & Lt; / Item & gt; & Lt; Items & gt; & Lt; ID & gt; 4 & lt; / Id & gt; & Lt; Group_id & gt; 2 & lt; / Group_id & gt; & Lt; Color & gt; Cyan & lt; / Color & gt; & Lt; City & gt; Washington & lt; / City & gt; & Lt; / Item & gt; & Lt; Items & gt; & Lt; ID & gt; 5 & ​​lt; / Id & gt; & Lt; Group_id & gt; 2 & lt; / Group_id & gt; & Lt; Color & gt; Gray & lt; / Color & gt; & Lt; City & gt; Colorado & lt; / City & gt; & Lt; / Item & gt; & Lt; / Item & gt;   

This XSLT can create the output you need

  & lt; Xsl: template match = "/" & gt; & Lt; Xsl: Choose Apply-Template = "Items" /> & Lt; / XSL: Templates & gt; & Lt; Xsl: template match = "item" & gt; & Lt; Xsl: Selecting each group = "item" group-rate = "group_id" & gt; & Lt; Xsl: element name = "item" & gt; & Lt; Xsl: element name = "group_id" & gt; & Lt; Xsl: Select Value = "Current-Group-Key ()" /> & Lt; / XSL: element & gt; & Lt; Xsl: each selection = "current-group ()" & gt; & Lt; Xsl: element name = "id" & gt; & Lt; Xsl: Select Value = "id" /> & Lt; / XSL: element & gt; & Lt; / XSL: for-each & gt; & Lt; / XSL: element & gt; & Lt; / XSL: for each group & gt; & Lt; / XSL: Templates & gt;   

Output

  & lt; Item & gt; & Lt; Group_id & gt; 1 & lt; / Group_id & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; ID & gt; 3 & lt; / Id & gt; & Lt; / Item & gt; & Lt; Items & gt; & Lt; Group_id & gt; 2 & lt; / Group_id & gt; & Lt; ID & gt; 4 & lt; / Id & gt; & Lt; ID & gt; 5 & ​​lt; / Id & gt; & Lt; / Item & gt; Edit: If you do not have an input file with all the items, but there are more files with a single item, then you can do something like this:  

>

  & lt; Xsl: variable name = "files" select = "archive ('file: ///path/to/files/item*.xml')" /> & Lt; Xsl: template match = "/" & gt; & Lt; Xsl: element name = "item" & gt; & Lt; Xsl: Select each group = "$ files / items" group = "group_id" & gt; & Lt; Xsl: element name = "item" & gt; & Lt; Xsl: element name = "group_id" & gt; & Lt; Xsl: Select Value = "Current-Group-Key ()" /> & Lt; / XSL: element & gt; & Lt; Xsl: each selection = "current-group ()" & gt; & Lt; Xsl: element name = "id" & gt; & Lt; Xsl: Select Value = "id" /> & Lt; / XSL: element & gt; & Lt; / XSL: for-each & gt; & Lt; / XSL: element & gt; & Lt; / XSL: for each group & gt; & Lt; / XSL: element & gt; & Lt; / XSL: Templates & gt;   



No comments:

Post a Comment