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 14th, 2009, 11:42 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Retreiving single record in XSLT

Hi All,

I have a XML document which contains duplicate data in the elements.

What i want in xslt just to retreive unique date for displaying.

For example: In XML there are two record with the same data i just want to retireive one records from XML in XSLT.

or is there any way to restrict duplicate record using XML Schema.

Below is my XML :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Users>
    <User>       
               
        <firstname>john</firstname>
        <lastname>walker</lastname>
                   
    </User>
    <User>    
        <firstname>john</firstname>
        <lastname>walker</lastname>
    </User>
</Users>
Below is the XSLT in that i am retrieving data :

Code:
<xsl:template match="user">
        <xsl:call-template name="user">
            <xsl:with-param name="firstname">
                <xsl:apply-templates select="./firstname"/>
            </xsl:with-param>
            <xsl:with-param name="lastname">
                <xsl:apply-templates select="./lastname"/>
            </xsl:with-param>
 </xsl:call-template>
</xsl:template>
 
<xsl:template name="user">
        <xsl:param name="firstname"/>
        <xsl:param name="lastname"/>
 
        <!-- Rest of the code goes here -->         
        
</xsl:template>
How to retrive one record if records are duplicate?

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

Google for "XSLT grouping". In XSLT 2.0 there's an instruction xsl:for-each-group which makes this easy. In XSLT 1.0 there's a coding technique called Muenchian grouping. In either case, you can use concat(firstname, '|', lastname) as the grouping key.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 14th, 2009, 11:52 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 2.0 you have xsl:for-each-group to group items, then you could process only the first item in each group to avoid duplicates e.g.
Code:
<xsl:template match="Users">
  <xsl:for-each-group select="User" group-by="concat(firstname, '|', lastname)">
    <!-- here . will select the first item in each group so you could for instance output that with e.g -->
    <xsl:copy-of select="."/>
  </xsl:for-each-group>
</xsl:template>
With XSLT 1.0 you can use Muenchian grouping to group items and then again you could process the first item in each group e.g.
Code:
<xsl:key name="k1" match="User" use="concat(firstname, '|', lastname)"/>

<xsl:template match="Users">
  <xsl:for-each select="User[generate-id() = generate-id(key('k1', concat(firstname, '|', lastname))[1])]">
      <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in appending a single record. prashanthmcr SQL Language 0 December 14th, 2005 08:47 PM
How do I append a single record ??? prashanthmcr SQL Language 3 December 14th, 2005 05:40 PM
"Single-Record" Recordset Wyatt70 Classic ASP Databases 2 November 18th, 2003 01:20 PM
Display a single record.. PLEASE HELP TnTandyO Classic ASP Databases 26 September 9th, 2003 10:34 AM





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