Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Getting distinct values

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 3/29/2008 2:22:00 PM

chris fellows wrote:

> Output XML required:
> <Transformed>
>     <Record Id="1">
>         <ColValuesA>
>             <ColA>A1</ColA>
>             <ColA>A2</ColA>
>         </ColValuesA>
>         <ColValuesB>
>             <ColB>B1</ColB>
>             <ColB>B2</ColB>
>             <ColB>B3</ColB>
>         </ColValuesB>
>  </Record>
>  <Record Id="2">
>     ...more...
>  </Record>
> </Transformed> 

Here is an XSLT 2.0 stylesheet that uses xsl:for-each-group and the 
distinct-values function:

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

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

   <xsl:template match="Root">
     <Tranformed>
       <xsl:for-each-group select="Record" group-by="@Id">
         <Record Id="{current-grouping-key()}">
           <ColValuesA>
             <xsl:for-each select="distinct-values(current-group()/ColA)">
               <ColA>
                 <xsl:value-of select="."/>
               </ColA>
             </xsl:for-each>
           </ColValuesA>
           <ColValuesB>
             <xsl:for-each select="distinct-values(current-group()/ColB)">
               <ColB>
                 <xsl:value-of select="."/>
               </ColB>
             </xsl:for-each>
           </ColValuesB>
         </Record>
       </xsl:for-each-group>
     </Tranformed>
   </xsl:template>

</xsl:stylesheet>

You can use XSLT 2.0 with Saxon 9 <URL:http://saxon.sourceforge.net/>, 
with Gestalt <URL:http://gestalt.sourceforge.net/> and with AltovaXML 
tools <URL:http://www.altova.com/altovaxml.html>

If you can't use XSLT 2.0 nevertheless then here is an XSLT 1.0 solution:

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

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

   <xsl:key name="by-id" match="Record" use="@Id"/>

   <xsl:key name="by-a" match="ColA" use="concat(../@Id, '_', .)"/>

   <xsl:key name="by-b" match="ColB" use="concat(../@Id, '_', .)"/>

   <xsl:template match="Root">
     <Transformed>
       <xsl:for-each select="Record[generate-id() = 
generate-id(key('by-id', @Id)[1])]">
         <Record Id="{@Id}">
           <ColValuesA>
             <xsl:for-each
               select="key('by-id', @Id)/ColA[generate-id() = 
generate-id(key('by-a', concat(../@Id, '_', .))[1])]">
                 <ColA>
                   <xsl:value-of select="."/>
                 </ColA>
             </xsl:for-each>
           </ColValuesA>
           <ColValuesB>
             <xsl:for-each
               select="key('by-id', @Id)/ColB[generate-id() = 
generate-id(key('by-b', concat(../@Id, '_', .))[1])]">
                 <ColB>
                   <xsl:value-of select="."/>
                 </ColB>
             </xsl:for-each>
           </ColValuesB>
         </Record>
       </xsl:for-each>
     </Transformed>
   </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent