Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 10th, 2011, 03:25 PM
Authorized User
 
Join Date: Nov 2009
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
Default for-each-group and differing attributes values

Hello,

I was hoping someone might be able to provide some advice for the following problem.

Xsl:for-each-group is being used to create a list of items with unique values. The difficulty is the attributes of any element will not be processed unless it belongs to the first instance.

For example

The XML
Code:
<group>
 <supplyli>
    <supply change=”add”>item x </supply>
    <supply>item y </supply>
    <supply>item z </supply>
    <supply>item x </supply>
    <supply change=”add”>item y </supply>
    <supply>item z </supply>
  </supplyli>
</group>
is being processed by the xslt
Code:
<xsl:for-each-group select="descendant::supplyli/supply" group-by=".">
  <fo:block>
    <xsl:if test="@change='modify' or @change='add'">
      <xsl:call-template name="rev.bar"/>
    </xsl:if>
    <xsl:apply-templates/>
  </fo:block>
</xsl:for-each-group>
The result is that item x, item y, and item z are each processed, and appear only once. In addition, rev.bar gets called for item x as the first instance contains the change attribute, but rev.bar does not get called for item y as its first instance does not.

The appearance in the output pdf would be

item x |
item y
item z

[ | being the output from rev.bar]

The desired outcome would be to process the two x’s and y’s differently because of their differing attributes, leading to the following output

item x |
item y
item x
item y |
item z

Thank you for reviewing the issue. Any suggestions would be appreciated.
 
Old March 10th, 2011, 03:50 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You just need to change the group-by attribute to be something that you actually want to group by, e.g. group-by="concat(.,@change)"

Sam
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
Kenneth.Dougherty (March 11th, 2011)
 
Old March 11th, 2011, 04:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First, as Sam says, if you want elements with differing attributes to go in different groups then you need to put them in different groups, by choosing a grouping key that distinguishes them. How to do that depends on a better knowledge of your data than you've given us.

Alternatively, you might want to keep the grouping as it is, but then when processing each group look at what you find in it (using current-group()) rather than only looking at the first element in the group. For example, you could do a second level of grouping based on the change attribute:

Code:
<xsl:for-each-group select="descendant::supplyli/supply" group-by=".">
  <fo:block>
    <xsl:for-each-group select="current-group()" select="(@change, '')[1]">
       ...
    </xsl:for-each-group>
  </fo:block>
</xsl:for-each-group>
The inner grouping key here will create one group for change="add", one for change="delete", etc, and one for elements with no @change attribute.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Kenneth.Dougherty (March 11th, 2011)
 
Old March 11th, 2011, 01:30 PM
Authorized User
 
Join Date: Nov 2009
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thank you for the help, Sam and Michael. Sam's solution worked for this issue, and Michael's suggestions helped with a similar problem.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying without attributes with default values in dtd mrame XSLT 2 October 28th, 2009 08:10 AM
XSLT:How to loop through nodes and retrieve attributes values based on some condition smandeh XSLT 2 March 24th, 2009 01:50 PM
Grouping based on attributes values Chamkaur XSLT 4 June 21st, 2006 05:51 AM
Generating differing dropdowns in a repeater? lancer ASP.NET 2.0 Basics 7 May 30th, 2006 02:01 PM
Group XML by Attribute values? NotesSensei XSLT 4 July 14th, 2004 11:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.