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 12th, 2013, 06:37 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Limit Unique (Distinct) Element Returns

Hello.

In my XML document, I have multiple <UMPREAUTHID> elements.
This element is assigned to each <DETAILLINE> and may have a unique or duplicate value.

Using XSLT Version 1.0 I'm using the following to return only the unique values of <UMPREAUTHID>.

Code:
<xsl:for-each select="DETAIL/DETAILLINE">
  <fo:inline color="#000000">
    <xsl:if test="UMPREAUTHID[.!='']>
      <xsl:if test="not(UMPREAUTHID[.=parent::*/preceding-sibling::*/UMPREAUTHID])">
        <xsl:value-of select="UMPREAUTHID"/>
        <fo:inline font-size="4pt"> </fo:inline>
      </xsl:if>
    </xsl:if>
  </fo:inline>
</xsl:for-each>
For example, here are some values:
<UMPREAUTHID>UMPREAUTH_NBR1</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR2</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR1</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR3</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR4</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR5</UMPREAUTHID>
<UMPREAUTHID>UMPREAUTH_NBR3</UMPREAUTHID>

However, I want to limit the return of unique <UMPREAUTHID> values to only three values. So, using the above example values, I only want to return
UMPREAUTH_NBR1 UMPREAUTH_NBR2 UMPREAUTH_NBR3

Is there a way to accomplish this?

I would also like to replace the space between each <UMPREAUTHID> with a comma but I can't have a comma after the last <UMPREAUTHID> displayed. So,
Code:
    <fo:inline font-size="4pt"> </fo:inline>
would be replaced with
Code:
    <fo:inline>,</fo:inline>
I'm not sure how to do prevent the last comma from displaying.

Any help will be greatly appreciated!

Thanks,
Rita

Last edited by ritagr; March 12th, 2013 at 07:28 PM..
 
Old March 13th, 2013, 06:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It should suffice to move the if conditions into predicates and then add a predicate with position() &lt; 4 e.g.
Code:
<xsl:for-each select="DETAIL/DETAILLINE[UMPREAUTHID[.!='']][not(UMPREAUTHID[.=parent::*/preceding-sibling::*/UMPREAUTHID])][position() &lt; 4]">
  <fo:inline color="#000000">
        <xsl:value-of select="UMPREAUTHID"/>
        <fo:inline font-size="4pt"> </fo:inline>
  </fo:inline>
</xsl:for-each>
As for the comma, use
Code:
<xsl:for-each  select="DETAIL/DETAILLINE[UMPREAUTHID[.!='']][not(UMPREAUTHID[.=parent::*/preceding-sibling::*/UMPREAUTHID])][position()  &lt; 4]">
  <fo:inline color="#000000">
        <xsl:if test="position() > 1"><fo:inline font-size="4pt">, </fo:inline></xsl:if>
        <xsl:value-of select="UMPREAUTHID"/>      
  </fo:inline>
</xsl:for-each>
__________________
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:
ritagr (March 15th, 2013)
 
Old March 13th, 2013, 11:23 AM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Limit Unique (Distinct) Element Returns

Hi Martin,

Thank you so much for your AWESOME help.
It's so great the way you combined the two tests into the "for-each" statement. Also, the way you placed the test to print the comma before printing
the element - I would never have thought of doing it that way!!

Many thanks,
Rita





Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify element based on the element data/content ROCXY XSLT 7 September 8th, 2011 02:48 AM
Setting unique element restriction tombliboo XML 3 February 12th, 2011 03:35 PM
Chapter 9: Element[user control] is not a known element Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 20th, 2009 07:31 AM
How to determine if an element value is unique? mphare XSLT 2 November 17th, 2008 12:12 PM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM





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