[XML4Lib] Problem with sorting and de-duplication in XSLT
Schlosser, Melanie Brynn
mschloss at indiana.edu
Thu Mar 22 10:05:17 EST 2007
Thanks, Con, and thanks to everyone else for the suggestions! The
approach below fixed both problems. :)
Melanie
Quoting Conal Tuohy <Conal.Tuohy at vuw.ac.nz>:
> Hi Melanie
>
> You need to use normalize-space() both in the grouping expression AND in
> the key, in order for the de-duplication to work.
>
> You need to select ALL the orgName elements in the document and apply
> templates to all those elements under the control of an xsl:sort, and
> that sort order has to use normalize-space() as well.
>
> Cheers!
>
> Con
>
> Try this:
>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
>
> <!--The first two templates are just to make sure the initial
> matching is working-->
> <xsl:template match="/">
> <foundRoot>
> <xsl:apply-templates/>
> </foundRoot>
> </xsl:template>
>
> <xsl:template match="text">
> <foundText>
> <xsl:apply-templates select=".//orgName">
> <xsl:sort select="normalize-space()"/>
> </xsl:apply-templates>
> </foundText>
> </xsl:template>
>
> <xsl:key name="orgName" match="orgName" use="normalize-space()"
> />
>
> <xsl:template match="orgName[count(. | key('orgName',
> normalize-space())[1]) = 1]">
> <Organization>
> <xsl:value-of select="normalize-space()" />
> </Organization>
> </xsl:template>
>
> <xsl:template match="text()"/>
>
> </xsl:stylesheet>
>
More information about the XML4Lib
mailing list