Fine, here's one with the irellevant data left out.
<people>
<person>
<name>Johnson</name>
<city>Boston</city>
<date>1965-02-05</date>
</person>
<person>
<name>Smit</name>
<city>Lutjebroek</city>
<date>1975-04-06</date>
</person>
<!-- etc. -->
</people>
Desired output (I get it this far)
Boston
Johnons
Peterson
Ackerman
Lutjebroek
Smit
Timmerman
de la Frost
Bos
Amsterdam
Lubbers
Veensma
This goes on as long as there are nodes in the original XML
What I want is to reduce the number names output to, let's say 5 in
total. This would look like:
Boston
Johnons
Peterson
Ackerman
Lutjebroek
Smit
Timmerman
An maybe reduce it to 2 per city. Which will look like:
Boston
Johnons
Peterson
Lutjebroek
Smit
Timmerman
Amsterdam
Lubbers
Veensma
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
<xsl
utput method="text"/>
<xsl:key name="kdistCity" match="city" use="."/>
<xsl:key name="kNamesByCity" match="name" use="../city"/>
<xsl:template match="/">
<xsl:variable name="vCities" select=
"/*/*/city[generate-id()
=
generate-id(key('kdistCity', .)[1])
]"/>
<xsl:call-template name="dispNames">
<xsl:with-param name="pCities" select="$vCities"/>
<xsl:with-param name="pBornAfter" select="19650201"/>
<xsl:with-param name="pMaxNames" select="6"/>
<xsl:with-param name="pMaxNamesPerCity" select="2"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="dispNames">
<xsl
aram name="pCities" select="/.."/>
<xsl
aram name="pBornAfter" select="0"/>
<xsl
aram name="pMaxNames" select="1000"/>
<xsl
aram name="pMaxNamesPerCity" select="100"/>
<xsl
aram name="ptotalDisplayed" select="0"/>
<xsl:variable name="vmore2Display"
select="$pMaxNames - $ptotalDisplayed"/>
<xsl:variable name="vthisCity" select="$pCities[1]"/>
<xsl:variable name="vnamesInThisCity" select=
"key('kNamesByCity', $vthisCity)
[translate(../date, '-', '')$pBornAfter
]"/>
<xsl:variable name="vcntNamesInCity"
select="count($vnamesInThisCity)"/>
<xsl:if test="$vmore2Display > 0 and $pCities">
<xsl:variable name="vdispLimit" select=
"$pMaxNamesPerCity * ($vmore2Display > $pMaxNamesPerCity)
+
$vmore2Display * (not($vmore2Display > $pMaxNamesPerCity))
"/>
<xsl:value-of select="concat('
', $vthisCity)"/>
<xsl:for-each select="$vnamesInThisCity">
<xsl:sort select="translate(../date, '-', '')"/>
<xsl:if test="position() <= $vdispLimit">
<xsl:value-of select="concat('
', .)"/>
</xsl:if>
</xsl:for-each>
<xsl:call-template name="dispNames">
<xsl:with-param name="pCities" select="$pCities[position() > 1]"/>
<xsl:with-param name="pBornAfter" select="$pBornAfter"/>
<xsl:with-param name="pMaxNames" select="$pMaxNames"/>
<xsl:with-param name="pMaxNamesPerCity" select="$pMaxNamesPerCity"/>
<xsl:with-param name="ptotalDisplayed" select=
"$ptotalDisplayed
+ $vdispLimit * ($vcntNamesInCity > $vdispLimit )
+ $vcntNamesInCity *(not($vcntNamesInCity > $vdispLimit))"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on this source.xml:
<people>
<person>
<name>Johnson</name>
<city>Boston</city>
<date>1965-02-05</date>
</person>
<person>
<name>Smit</name>
<city>Lutjebroek</city>
<date>1975-04-06</date>
</person>
<person>
<name>Peterson</name>
<city>Boston</city>
<date>1965-02-05</date>
</person>
<person>
<name>Ackerman</name>
<city>Boston</city>
<date>1965-02-05</date>
</person>
<person>
<name>Timmerman</name>
<city>Lutjebroek</city>
<date>1975-04-06</date>
</person>
<person>
<name>de la Frost</name>
<city>Lutjebroek</city>
<date>1975-04-06</date>
</person>
<person>
<name>Bos</name>
<city>Lutjebroek</city>
<date>1975-04-06</date>
</person>
<person>
<name>Lubbers</name>
<city>Amsterdam</city>
<date>1975-04-06</date>
</person>
<person>
<name>Veensma</name>
<city>Amsterdam</city>
<date>1975-04-06</date>
</person>
<!-- etc. -->
</people>
produces the wanted result:
Boston
Johnson
Peterson
Lutjebroek
Smit
Timmerman
Amsterdam
Lubbers
Veensma
It has a total limit of 6 names and a limit of maximum 2 names per city.
Hope this helped.
Cheers,
Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,
http://fxsl.sourceforge.net/ -- the home of FXSL
Resume:
http://fxsl.sf.net/DNovatchev/Resume/Res.html