Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Thu, 29 May 2008 07:26:09 -0700 (PDT),    group: microsoft.public.xsl        back       


How to get the DISTINCT values?   
I use xsl version 2 to transform xml data
This is the input xml file

<?xml version="1.0"?>
<csv xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <line>
      <field>210.84</field>
      <field>0</field>
      <field>1</field>
      <field>2</field>
      <field>3</field>
   </line>
   <line>
      <field>210.84</field>
      <field>0</field>
      <field>1</field>
      <field>2</field>
      <field>3</field>
   </line>
   <line>
      <field>210.84</field>
      <field>6</field>
      <field>5</field>
      <field>4</field>
      <field>3</field>
   </line>
   <line>
      <field>210.84</field>
      <field>6</field>
      <field>5</field>
      <field>4</field>
      <field>3</field>
   </line>
   <line>
      <field>210.84</field>
      <field>6</field>
      <field>5</field>
      <field>4</field>
      <field>3</field>
   </line>
</csv>

We need to get the DISTINCT values in the field[2] in each element
<line>
We have for this example two distinct value which are 0 and 6 in the
elements <field[2]>. We need to have an output for these distinct
values and the two elements following them. The output should be:

<?xml version="1.0"?>
<csv xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <line>
      <field>0</field>
      <field>1</field>
      <field>2</field>
   </line>
   <line>
      <field>6</field>
      <field>5</field>
      <field>4</field>
   </line>
</csv>
date: Thu, 29 May 2008 07:26:09 -0700 (PDT)   author:   unknown

Re: How to get the DISTINCT values?   
heshamelesawy@gmail.com wrote:

> We need to get the DISTINCT values in the field[2] in each element
> <line>
> We have for this example two distinct value which are 0 and 6 in the
> elements <field[2]>. We need to have an output for these distinct
> values and the two elements following them. 

You can use xsl:for-each-group in XSLT 2.0 as follows:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="csv">
      <xsl:copy>
        <xsl:for-each-group select="line" group-by="field[2]">
          <xsl:apply-templates select="current-group()[1]"/>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="line">
      <xsl:copy>
        <xsl:apply-templates select="field[2] | field[3] | field[4]"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="field">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Thu, 29 May 2008 17:15:43 +0200   author:   Martin Honnen

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us