Skip to Content

Introduction



Everybody who ever did some XML processing, with or without XI, knows, that the main standard operations which are necessary to transform (map) XML messages are selection, aggregation and grouping.

Before going to practical cases and examples, I would like to clarify some differences of these operations by comparing the XML world with the more ‘traditional’ world of relational databases, although this comparison cannot be a good one due to the vast difference between the hierarchical tree structure of XML and the more or less ‘flat earth’ model of RDBS tables (ignoring the many efforts to ‘pump up’ SQL to handle tree structures).

    • Selection:


In the database world the well known SQL SELECT statement is used. In XSLT we have XSLT functions and XPATH expressions to access data selectively.

    1. Aggregation:


Aggregated data access is done in SQL with the help of the aggregate functions like for example MIN,MAX,AVG,SUM. In XML this can also be done with a mixture of XSLT funtions and XPATH expressions.

    1. Grouping:


Grouping data can be defined as a mapping from a set of tuples to a set of sets of tuples (oops: hierarchy!).
Grouping in relational databases is therefore not separable from aggregation (the inversion of the definition above) because of the fact, that the ‘target data type’ of grouping is always ‘tree’ and not ‘table’ This is the reason, why the GROUP-BY and HAVING operators in SQL are only allowed together with some aggregate operator to guarantee a table result (set of tuples). Grouping is always a two-step operation which first splits a collection of data into not necessarily disjoint subset-trees of data and second order these subset-trees in some manner.
</li>
</ul>

A Simple Example


The Muenchian Method



This grouping method goes back to and is called after Steve Muench, Oracle’s primary representative to the W3C XSL Working Group. It is an efficient method to get the grouping done by using <xsl:keys> to avoid other XSLT constructs like tracing all preceding siblings to get the grouping done, which is, at least for large data, very time-consuming. Let’s have a look at the transformation, which groups the positions of the example order according to their quantities:


<xsl:transform version=”1.0″
  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform


First, a key for the MENGE field (order quantity) of the E1EDP01 tag is defined. By using this key, every position can be accessed easily by the order quantity. Secondly the unique list of all different quantities occuring in the sample data is looped thru by the first <xsl:for-each> statement creating a <POSITIONGROUP> tag with the quantity attribute. Finally, in a second loop all position data matching the quantity in the group is collected and wrapped in a <POSITION> tag. This is done by the simple key access in the second <xsl:for-each> statement. The result of this transform looks like this:


Grouping with XSLT 2.0



With XSLT 2.0, grouping is much easier than in 1.0. I just touch the necessary changes to our example XSLT to get the same result with new new grouping features. Who is interested may look at the W3C documentation . The XSLT 2.0 stylesheet looks like this:


<xsl:transform version=”2.0″
  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform


Instead of defining a key for the grouping like in the Muenchian method, the grouping can be coded straightforward with the <xsl:for-each-group> command. Inside the loop every group member can be accessed by a second <xsl:for-each> loop using the selection current-group(). As already said the result is the same desired XML just like with the Muenchian Method.
</p>

Conclusion


Grouping is an interesting issue and often reoccurring need in XML transformations. With XSLT 2.0 it is directly supported and substitutes the former state-of-the-art Muenchian Method.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Badari Garre
    Dirk,

    This is an excellent blog depicting the grouping using XSLT 2.0. 

    I tried doing this on my XI (3.0 SP13), but it seems mine only supports XSLT 1.1.  How can I implement 2.0 support? Can I implement a 3rd party vendor like SAXON on XI ?

    I know the best place for asking this would be on forums. But I couldn’t anyone with a knowledgeable answer – other than just saying XI doesn’t support 2.0!

    Thanks,
    Badari

    (0) 
    1. Dirk Roeckmann Post author
      Hello Badari,

      I simply do not know, if and when XI will support XSLT 2.0. I think it depends on which Java stack release lies behind the XI. Lets wait for future PI releases.

      But you are right: Use packages like SAXON and integrate them in an adapter aodule for XI. With this it would be posssible to transform messages with XSLT 2.0.

      Search the blogs & forums here on SDN. There is a lot of stuff about developing adapter modules.

      (0) 
  2. Chintan Doshi
    Dirk, excellent job,,,

    I am in a situation hope you can help me with it.

    I have data coming in from a XML file.

    Data in this file is at 3 levels.
    1st level contains the header info. Next two levels contain the transaction data info.
    Ex: Level 2 has the product name and level 3 has amount and corresponding time period. To be more specific Level 2 has all the products and Level 3 has the corresponding transaction amount and calday for that transaction….

    Any tips on how to design a datasource in this case?
    If just add all the fields at both levels to the data source then the generated WSDL file/web service considers all the fields coming at the same level.

    Thanks,
    sam

    (0) 

Leave a Reply