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 November 23rd, 2009, 03:08 PM
Authorized User
 
Join Date: Nov 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Red face labeling duplicates

say I have the following

{ A, B, B, C, D, E, E, F, F, F, G }

now that should be converted into

{ A, B1, B2, C, D, E1, E2, F1, F2, F3, G }

In procedural piece of cake. Should be easy enough in XSLT I guess, but not for me!
In the problem at hand the sets are really elements and the labels A,B.. the values of their name attributes.

Last edited by dexter62; November 23rd, 2009 at 03:12 PM..
 
Old November 23rd, 2009, 03:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A standard grouping problem, therefore very easy in XSLT 2.0, rather harder in XSLT 1.0.

In 2.0 it's simply

Code:
<xsl:for-each select="*" group-adjacent=".">
  <xsl:for-each select="current-group()">
     <xsl:value-of select="concat(., position())"/>
  </
</
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 24th, 2009, 11:20 AM
Authorized User
 
Join Date: Nov 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Cool grouping problem...but XSLT 1.1?

Quote:
Originally Posted by mhkay View Post
A standard grouping problem, therefore very easy in XSLT 2.0, rather harder in XSLT 1.0.

In 2.0 it's simply

Code:
<xsl:for-each select="*" group-adjacent=".">
  <xsl:for-each select="current-group()">
     <xsl:value-of select="concat(., position())"/>
  </
</
Looks great! However, we currently use no XSLT 2 but 1.1 and I am not in the position to change this. Is there any neat way to go about it in 1.1?

Thanks again.
 
Old November 24th, 2009, 11:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Search for positional grouping. Sorry, I don't write code in XSLT 1.0 any more for cases where 2.0 is so much easier - if you insist on using old technology, you'll have to take the pain.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 24th, 2009, 12:10 PM
Authorized User
 
Join Date: Nov 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Arrow understandable but..

Quote:
Originally Posted by mhkay View Post
Search for positional grouping. Sorry, I don't write code in XSLT 1.0 any more for cases where 2.0 is so much easier - if you insist on using old technology, you'll have to take the pain.
Yeah, I see what you mean. I'll forward the message...
 
Old November 24th, 2009, 12:51 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If the values (e.g. A, B, C) in the input are sorted and already adjacent then you can simply group by that value with Muenchian grouping as follows:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:key name="k1" match="foo" use="@name"/>
  
  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each select="foo[generate-id() = generate-id(key('k1', @name)[1])]">
        <xsl:variable name="current-group" select="key('k1', @name)"/>
        <xsl:choose>
          <xsl:when test="count($current-group)&gt; 1">
            <xsl:for-each select="$current-group">
              <foo name="{@name}{position()}"/>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
       </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
That will transform
Code:
<root>
  <foo name="A"/>
  <foo name="B"/>
  <foo name="B"/>
  <foo name="C"/>
  <foo name="D"/>
  <foo name="D"/>
  <foo name="D"/>
  <foo name="E"/>
  <foo name="E"/>
  <foo name="F"/>
</root>
to
Code:
<root>
   <foo name="A"/>
   <foo name="B1"/>
   <foo name="B2"/>
   <foo name="C"/>
   <foo name="D1"/>
   <foo name="D2"/>
   <foo name="D3"/>
   <foo name="E1"/>
   <foo name="E2"/>
   <foo name="F"/>
</root>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
dexter62 (November 27th, 2009)
 
Old November 26th, 2009, 09:21 AM
Authorized User
 
Join Date: Nov 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Thumbs up looks great

Hi certainly this looks like it. Sorry I had not had the time to check earlier, I will be trying this later today. Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Getting duplicates mat41 SQL Language 1 December 15th, 2008 02:03 AM
Yes to duplicates MMJiggy62 Beginning VB 6 3 July 14th, 2006 11:51 AM
Duplicates in XSLT d_sathish XSLT 1 November 9th, 2005 11:45 AM
Delete duplicates? boson SQL Language 2 August 11th, 2004 04:47 PM
Duplicates ashley_y Access 11 August 14th, 2003 03:41 PM





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