Grouping by multiple fields in XSL?

Posted on 2007-07-27
Last Modified: 2013-11-18
I am using the following to group a "single" field:
      <xsl:key name="ShipmentGrpKey" match="Containers/Container" use="@ShipmentKey"/>
      <xsl:template match="/">
                  <xsl:for-each select="Containers/Container[count(. | key('ShipmentGrpKey',@ShipmentKey)[1]) = 1]">

Now, I want to group by multiple fields:

I tried:

<xsl:key name="NewShipmentKey" match="Shipments/Shipment" use="@ShipmentKey"/>
<xsl:key name="Agent" match="Shipments/Shipment" use="@ExtnAgentCode"/>
<xsl:template match="/">
                  <xsl:for-each select="Shipments/Shipment[count(. | key('NewShipmentKey',@ShipmentKey)[1]| key('Agent',@ExtnAgentCode)[1]) = 1]">

But it doesnt seem to work! Any suggestions?
Question by:rajivraj
    LVL 13

    Expert Comment

    Just a tip (see Multiple Level Grouping):
    LVL 60

    Accepted Solution

    What exactly do you mean with multiple fields?

    1. nested: field 2 is grouped inside field 1
    Then you need two keys, as you have, but you will need an extra for-each
    <xsl:key name="NewShipmentKey" match="Shipments/Shipment" use="@ShipmentKey"/>
    <xsl:key name="Agent" match="Shipments/Shipment" use="@ExtnAgentCode"/>
          <xsl:template match="/">
                      <xsl:for-each select="Shipments/Shipment[count(. | key('NewShipmentKey',@ShipmentKey)[1]) = 1]">
                           <xsl:variable name="thisShipmentKey" select="@ShipmentKey" />
                           <xsl:for-each select="../Shipment[@ShipmentKey = $thisShipmentKey][count(. | key('ShipmentGrpKey',@ExtnAgentCode)[@ShipmentKey = $thisShipmentKey][1]) = 1]">

    make sure you add the grouping filter of the outer loop ([@ShipmentKey = $thisShipmentKey]) to the elements in the inner loop
    also take into account that Shipment is the context node of the outer loop
    so you need to escape from that

    I feel that using Muenchian with count is clumsier than using it with generate-id() when you are nesting

    2. you need to group on the combined key.
    If that is the case, you don't need two keys, but a combined one
    <xsl:key name="NewShipmentKey" match="Shipments/Shipment" use="concat(@ShipmentKey, '_', @ExtnAgentCode)"/>

    posting an example XML and a required result would help in providing more detailed feedback



    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now