?
Solved

Grouping by multiple fields in XSL?

Posted on 2007-07-27
2
Medium Priority
?
172 Views
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="/">
            <Orderss>
                  <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="/">
      <Shipments>
                  <xsl:for-each select="Shipments/Shipment[count(. | key('NewShipmentKey',@ShipmentKey)[1]| key('Agent',@ExtnAgentCode)[1]) = 1]">

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

Expert Comment

by:R7AF
ID: 19581604
Just a tip (see Multiple Level Grouping):
http://www.jenitennison.com/xslt/grouping/
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 375 total points
ID: 19591111
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="/">
            <Orderss>
                  <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

cheers

Geert
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

850 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