[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] Occurences of comma-separated values
Subject: Re: [xsl] Occurences of comma-separated values
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 29 Jul 2006 21:32:49 +0530
|
Here is the XSLT 1.0 stylesheet for this (using an extension function,
node-set):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:common="http://exslt.org/common">
<xsl:output method="text" />
<xsl:key name="x" match="token" use="." />
<xsl:template match="/list">
<xsl:variable name="rtf">
<xsl:for-each select="*">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="@ids" />
<xsl:with-param name="delim" select="','" />
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="common:node-set($rtf)/token[generate-id() =
generate-id(key('x', .)[1])]">
<xsl:sort select="." />
<xsl:value-of select="." />: <xsl:value-of select="count(key('x',
.))" /><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="string" />
<xsl:param name="delim" />
<xsl:choose>
<xsl:when test="contains($string, $delim)">
<token><xsl:value-of select="substring-before($string, $delim)" /></token>
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="substring-after($string,
$delim)" />
<xsl:with-param name="delim" select="$delim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<token><xsl:value-of select="$string" /></token>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With XSLT 2.0, the solution will be simpler.
Regards,
Mukul
http://gandhimukul.tripod.com
On 7/29/06, Smoother@xxxxxx <Smoother@xxxxxx> wrote:
For instance, I have the following XML-File:
<list>
<item1 ids="a,b,c,e" />
<item2 ids="b,c,d" />
<item3 ids="a,c,d,e" />
<item4 ids="e,f" />
<item5 ids="a,c,d,e,g" />
</list>
I contemplate how to count the occurences of
(a) all ids (e.g. 'a') in the document and
(b) to count the occurences of a specific (known) id (e.g. 'f') in the document.
For case a the output should be like this:
a: 3
b: 2
c: 4
d: 3
e: 4
f: 1
g: 1
I started to learn XSL yesterday. For several other problems, I found an easily adaptable solution on the web, numerous time on this list here.
But for my current problem, there was no such help available.
Of course, I found here a similar posting which deals with occurences of equal nodes (http://www.stylusstudio.com/xsllist/200309/post20830.html), but I don't know how to adapt it. So please find a solution and help me and others who have the same problem.
Thanks in advance,
S. Renault
|