• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

counting the distincit nodes xslt

Hi there,

I have a small issue with my xslt code. The code worked properly before I started adding the element <total> which counts the total distinct element <search> in wordlist.xml.

Please have a look at the xslt <total> element and help me to crack it..

Cheers,
-------------xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="search" match="wordEntry" use="search"/>
    <xsl:template match="wordlist">
        <Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="translation.xsd">
            <from><xsl:value-of select="@from" /></from>
            <to><xsl:value-of select="@to" /></to>
			
			<total>
			<xsl:value-of select="wordEntry[generate-id() = generate-id(key('wordEntry', search)[1])]"/>
			</total>
            <xsl:apply-templates select="wordEntry[generate-id() = generate-id(key('search', search)[1])]"/>
        </Dictionary>
    </xsl:template>
    <xsl:template match="wordEntry">
        <translation initial="{substring(search ,1,1)}">
            <xsl:copy-of select="search"/>
            <counter>
                <xsl:value-of select="count(key('search', search))"/>
            </counter>
            <xsl:copy-of select="key('search', search)/replace"/>
        </translation>
    </xsl:template>
</xsl:stylesheet>

------ source wordlist.xml
<?xml version="1.0" encoding="UTF-8"?>
<wordlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="wordlist.xsd"
            from="en"
            to="es">
   <wordEntry>
      <search>A Clockwork Orange</search>
      <replace>La naranja mecánica</replace>
   </wordEntry>
   <wordEntry>
      <search>A Few Good Men</search>
      <replace>A Few Good Men</replace>
   </wordEntry>
   <wordEntry>
      <search>A Star Is Born</search>
      <replace>Ha nacido una estrella</replace>
   </wordEntry>
   <wordEntry>
      <search>Ab Urbe condita</search>
      <replace>Ab Urbe condita libri</replace>
   </wordEntry>
   <wordEntry>
      <search>Ab urbe condita</search>
      <replace>Ab urbe condita</replace>
   </wordEntry>
   <wordEntry>
      <search>Ab urbe condita</search>
      <replace>Ab urbe condita</replace>
   </wordEntry>
   <wordEntry>
      <search>Abel</search>
      <replace>Abel</replace>
   </wordEntry>
   <wordEntry>
      <search>Abel</search>
      <replace>Caín</replace>
   </wordEntry>
   <wordEntry>
      <search>Abel</search>
      <replace>Abel</replace>
   </wordEntry>
   <wordEntry>
      <search>Abel</search>
      <replace>Caín</replace>
   </wordEntry>
   <wordEntry>
      <search>Alyosha Karamazov</search>
      <replace>Los hermanos Karamazov</replace>
   </wordEntry>
   <wordEntry>
      <search>And Then There Were None</search>
      <replace>Diez negritos</replace>
   </wordEntry>
   <wordEntry>
      <search>Angst</search>
      <replace>Gaslight</replace>
   </wordEntry>
   <wordEntry>
      <search>Gaslight</search>
      <replace>Gaslight</replace>
   </wordEntry>
   <wordEntry>
      <search>Around The World In 80 Days</search>
      <replace>La vuelta al mundo en ochenta días</replace>
   </wordEntry>
   <wordEntry>
      <search>Around the World in Eighty Days</search>
      <replace>La vuelta al mundo en ochenta días</replace>
   </wordEntry>
   <wordEntry>
      <search>Batman &amp; Robin</search>
      <replace>Batman y Robin</replace>
   </wordEntry>
   <wordEntry>
      <search>Bomarzo</search>
      <replace>Bomarzo</replace>
   </wordEntry>
   <wordEntry>
      <search>Brave New World</search>
      <replace>Un mundo feliz</replace>
   </wordEntry>
   <wordEntry>
      <search>Casino Royale</search>
      <replace>Casino Royale</replace>
   </wordEntry>
   <wordEntry>
      <search>Cheaper by the Dozen</search>
      <replace>Más barato por docena</replace>
   </wordEntry>
   <wordEntry>
      <search>Cinderella</search>
      <replace>La Cenicienta</replace>
   </wordEntry>
   <wordEntry>
      <search>Da Vinci Code</search>
      <replace>El código Da Vinci</replace>
   </wordEntry>
   <wordEntry>
      <search>Dawn of the Dead</search>
      <replace>El amanecer de los muertos</replace>
   </wordEntry>
   <wordEntry>
      <search>Diary of a Chambermaid</search>
      <replace>Diario de una camarera</replace>
   </wordEntry>
   <wordEntry>
      <search>Dune</search>
      <replace>Dune</replace>
   </wordEntry>
   <wordEntry>
      <search>Evil Dead</search>
      <replace>Posesión infernal</replace>
   </wordEntry>
   <wordEntry>
      <search>Frankenstein</search>
      <replace>Frankenstein</replace>
   </wordEntry>
   <wordEntry>
      <search>Fyodor Karamazov</search>
      <replace>Los hermanos Karamazov</replace>
   </wordEntry>
   <wordEntry>
      <search>General San Martín Park</search>
      <replace>Parque General San Martín</replace>
   </wordEntry>
   <wordEntry>
      <search>Gentlemen Prefer Blondes</search>
      <replace>Los caballeros las prefieren rubias</replace>
   </wordEntry>
   <wordEntry>
      <search>Goldfinger</search>
      <replace>Goldfinger</replace>
   </wordEntry>
   <wordEntry>
      <search>Haitian Creole</search>
      <replace>Criollo haitiano</replace>
   </wordEntry>
   <wordEntry>
      <search>Haitian language</search>
      <replace>Criollo haitiano</replace>
   </wordEntry>
   <wordEntry>
      <search>Hamlet</search>
      <replace>Hamlet</replace>
   </wordEntry>
   <wordEntry>
      <search>House of Wax</search>
      <replace>La casa de cera</replace>
   </wordEntry>
   <wordEntry>
      <search>I, Robot</search>
      <replace>Yo, Robot</replace>
   </wordEntry>
   <wordEntry>
      <search>Il Gattopardo</search>
      <replace>El gatopardo</replace>
   </wordEntry>
   <wordEntry>
      <search>In Cold Blood</search>
      <replace>A sangre fría</replace>
   </wordEntry>
   <wordEntry>
      <search>Interview with the Vampire</search>
      <replace>Entrevista con el Vampiro</replace>
   </wordEntry>
   <wordEntry>
      <search>Ivan Karamazov</search>
      <replace>Los hermanos Karamazov</replace>
   </wordEntry>
   <wordEntry>
      <search>John Robert Schrieffer</search>
      <replace>John Robert Schrieffer</replace>
   </wordEntry>
   <wordEntry>
      <search>J Robert Schrieffer</search>
      <replace>John Robert Schrieffer</replace>
   </wordEntry>
   <wordEntry>
      <search>Joseph K</search>
      <replace>El proceso</replace>
   </wordEntry>
   <wordEntry>
      <search>Journey to the Center of the Earth</search>
      <replace>Viaje al centro de la Tierra</replace>
   </wordEntry>
   <wordEntry>
      <search>La Boheme</search>
      <replace>La Bohème</replace>
   </wordEntry>
   <wordEntry>
      <search>La Boheme</search>
      <replace>La Bohème</replace>
   </wordEntry>
   <wordEntry>
      <search>La bohème</search>
      <replace>La Bohème</replace>
   </wordEntry>
   <wordEntry>
      <search>La bohème</search>
      <replace>La Bohème</replace>
   </wordEntry>
   <wordEntry>
      <search>Les Miserables</search>
      <replace>Los miserables</replace>
   </wordEntry>
   <wordEntry>
      <search>Les Misérables</search>
      <replace>Los miserables</replace>
   </wordEntry>
   <wordEntry>
      <search>Librarians in the Name of the Rose</search>
      <replace>El nombre de la rosa</replace>
   </wordEntry>
   <wordEntry>
      <search>Lolita</search>
      <replace>Lolita</replace>
   </wordEntry>
   <wordEntry>
      <search>Lord of the Flies</search>
      <replace>El señor de las moscas</replace>
   </wordEntry>
   <wordEntry>
      <search>Lord of the Flies in popular culture</search>
      <replace>El señor de las moscas</replace>
   </wordEntry>
   <wordEntry>
      <search>M</search>
      <replace>M, el vampiro de Düsseldorf</replace>
   </wordEntry>
   <wordEntry>
      <search>Macbeth</search>
      <replace>Macbeth</replace>
   </wordEntry>
   <wordEntry>
      <search>Madame Bovary</search>
      <replace>Madame Bovary</replace>
   </wordEntry>
   <wordEntry>
      <search>Materialism</search>
      <replace>Materialismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Materialism</search>
      <replace>Materialismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Materialist</search>
      <replace>Materialismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Materialist</search>
      <replace>Materialismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Mathematica</search>
      <replace>Mathematica</replace>
   </wordEntry>
   <wordEntry>
      <search>Midsummer Night's Dream</search>
      <replace>El sueño de una noche de verano</replace>
   </wordEntry>
   <wordEntry>
      <search>Midsummer Night's Dream</search>
      <replace>El sueño de una noche de verano</replace>
   </wordEntry>
   <wordEntry>
      <search>Mountaineering</search>
      <replace>Montañismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Mountain climbing</search>
      <replace>Montañismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Alpinism</search>
      <replace>Montañismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Murder on the Orient Express</search>
      <replace>Asesinato en el Orient Express</replace>
   </wordEntry>
   <wordEntry>
      <search>Musketeers</search>
      <replace>Los tres mosqueteros</replace>
   </wordEntry>
   <wordEntry>
      <search>Myocardial infarction</search>
      <replace>Infarto</replace>
   </wordEntry>
   <wordEntry>
      <search>Myocardial infarction</search>
      <replace>Infarto de miocardio</replace>
   </wordEntry>
   <wordEntry>
      <search>Name of the Rose</search>
      <replace>El nombre de la rosa</replace>
   </wordEntry>
   <wordEntry>
      <search>Neverending Story</search>
      <replace>La historia interminable</replace>
   </wordEntry>
   <wordEntry>
      <search>Night Watch</search>
      <replace>La ronda de noche</replace>
   </wordEntry>
   <wordEntry>
      <search>Night Watch painting</search>
      <replace>La ronda de noche</replace>
   </wordEntry>
   <wordEntry>
      <search>Night of the Living Dead</search>
      <replace>La noche de los muertos vivientes</replace>
   </wordEntry>
   <wordEntry>
      <search>Nineteen Eighty-Four</search>
      <replace>Mil novecientos ochenta y cuatro</replace>
   </wordEntry>
   <wordEntry>
      <search>Northern Savonia</search>
      <replace>Savonia del Norte</replace>
   </wordEntry>
   <wordEntry>
      <search>Oath of the Horatii</search>
      <replace>Juramento de los Horacios</replace>
   </wordEntry>
   <wordEntry>
      <search>Oath of the Horatii</search>
      <replace>Juramento de los Horacios</replace>
   </wordEntry>
   <wordEntry>
      <search>Ocean's Eleven</search>
      <replace>Cuadrilla de los once</replace>
   </wordEntry>
   <wordEntry>
      <search>Ocean's Eleven</search>
      <replace>Ocean's Eleven</replace>
   </wordEntry>
   <wordEntry>
      <search>Oliver Twist</search>
      <replace>Oliver Twist</replace>
   </wordEntry>
   <wordEntry>
      <search>On Her Majesty's Secret Service</search>
      <replace>al servicio secreto de su majestad</replace>
   </wordEntry>
   <wordEntry>
      <search>Parque Central Complex</search>
      <replace>Torres de Parque Central</replace>
   </wordEntry>
   <wordEntry>
      <search>Parque Central Complex</search>
      <replace>Torres de Parque Central Caracas</replace>
   </wordEntry>
   <wordEntry>
      <search>Parque San Martin</search>
      <replace>Parque General San Martín</replace>
   </wordEntry>
   <wordEntry>
      <search>Parque San Martin</search>
      <replace>Parque General San Martín Mendoza</replace>
   </wordEntry>
   <wordEntry>
      <search>Persimmon</search>
      <replace>Diospyros kaki</replace>
   </wordEntry>
   <wordEntry>
      <search>Persimmon</search>
      <replace>Diospyros kaki</replace>
   </wordEntry>
   <wordEntry>
      <search>Persimmons</search>
      <replace>Diospyros kaki</replace>
   </wordEntry>
   <wordEntry>
      <search>Persimmons</search>
      <replace>Diospyros kaki</replace>
   </wordEntry>
   <wordEntry>
      <search>Peter Pan</search>
      <replace>Peter Pan</replace>
   </wordEntry>
   <wordEntry>
      <search>Phantom of the Opera</search>
      <replace>El Fantasma de la Ópera</replace>
   </wordEntry>
   <wordEntry>
      <search>Planet of the Apes</search>
      <replace>El planeta de los simios</replace>
   </wordEntry>
   <wordEntry>
      <search>Pride and Prejudice</search>
      <replace>Orgullo y prejuicio</replace>
   </wordEntry>
   <wordEntry>
      <search>Quality control</search>
      <replace>Control de calidad</replace>
   </wordEntry>
   <wordEntry>
      <search>Quality control</search>
      <replace>Control de calidad</replace>
   </wordEntry>
   <wordEntry>
      <search>Quebec city</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Quebec city</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Quebec city</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Quirinal</search>
      <replace>Quirinal</replace>
   </wordEntry>
   <wordEntry>
      <search>Quirinale</search>
      <replace>Quirinal</replace>
   </wordEntry>
   <wordEntry>
      <search>Quirinale</search>
      <replace>Quirinal</replace>
   </wordEntry>
   <wordEntry>
      <search>Québec City</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Québec City</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Québec City</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Québec City</search>
      <replace>Ciudad de Quebec</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionism</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionism</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionist</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionist</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionistic</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Reductionistic</search>
      <replace>Reduccionismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Roman Holiday</search>
      <replace>Vacaciones en Roma</replace>
   </wordEntry>
   <wordEntry>
      <search>Romeo and Juliet</search>
      <replace>Romeo y Julieta</replace>
   </wordEntry>
   <wordEntry>
      <search>Sense and Sensibility</search>
      <replace>Sensatez y sentimientos</replace>
   </wordEntry>
   <wordEntry>
      <search>Smerdyakov</search>
      <replace>Los hermanos Karamazov</replace>
   </wordEntry>
   <wordEntry>
      <search>Solaris</search>
      <replace>Solaris</replace>
   </wordEntry>
   <wordEntry>
      <search>St Mark's Cathedral</search>
      <replace>Basílica de San Marcos</replace>
   </wordEntry>
   <wordEntry>
      <search>St Mark's Cathedral</search>
      <replace>Basílica de San Marcos</replace>
   </wordEntry>
   <wordEntry>
      <search>The Brothers Karamazov</search>
      <replace>Los hermanos Karamazov</replace>
   </wordEntry>
   <wordEntry>
      <search>The Da Vinci Code</search>
      <replace>El código Da Vinci</replace>
   </wordEntry>
   <wordEntry>
      <search>The Evil Dead</search>
      <replace>Posesión infernal</replace>
   </wordEntry>
   <wordEntry>
      <search>The Exorcist</search>
      <replace>El Exorcista</replace>
   </wordEntry>
   <wordEntry>
      <search>The Fast and the Furious</search>
      <replace>The Fast and the Furious</replace>
   </wordEntry>
   <wordEntry>
      <search>The Fugitive</search>
      <replace>El fugitivo</replace>
   </wordEntry>
   <wordEntry>
      <search>The Great Gatsby</search>
      <replace>El gran Gatsby</replace>
   </wordEntry>
   <wordEntry>
      <search>Gatsby</search>
      <replace>El gran Gatsby</replace>
   </wordEntry>
   <wordEntry>
      <search>The Idiot</search>
      <replace>El idiota</replace>
   </wordEntry>
   <wordEntry>
      <search>The Island</search>
      <replace>La isla</replace>
   </wordEntry>
   <wordEntry>
      <search>The Italian Job</search>
      <replace>The Italian Job</replace>
   </wordEntry>
   <wordEntry>
      <search>The Jazz Singer</search>
      <replace>El cantante de jazz</replace>
   </wordEntry>
   <wordEntry>
      <search>The Leopard</search>
      <replace>El gatopardo</replace>
   </wordEntry>
   <wordEntry>
      <search>The Maltese Falcon</search>
      <replace>El halcón maltés</replace>
   </wordEntry>
   <wordEntry>
      <search>The Mummy</search>
      <replace>La Momia</replace>
   </wordEntry>
   <wordEntry>
      <search>The Name of the Rose</search>
      <replace>El nombre de la rosa</replace>
   </wordEntry>
   <wordEntry>
      <search>The Neverending Story</search>
      <replace>La historia interminable</replace>
   </wordEntry>
   <wordEntry>
      <search>The Old Man and the Sea</search>
      <replace>El viejo y el mar</replace>
   </wordEntry>
   <wordEntry>
      <search>The Phantom of the Opera</search>
      <replace>El Fantasma de la Ópera</replace>
   </wordEntry>
   <wordEntry>
      <search>The Picture of Dorian Gray</search>
      <replace>El retrato de Dorian Gray</replace>
   </wordEntry>
   <wordEntry>
      <search>The Ring</search>
      <replace>La señal</replace>
   </wordEntry>
   <wordEntry>
      <search>The Spy Who Loved Me</search>
      <replace>El espía que me amó</replace>
   </wordEntry>
   <wordEntry>
      <search>The Stranger</search>
      <replace>El extranjero</replace>
   </wordEntry>
   <wordEntry>
      <search>The Ten Commandments</search>
      <replace>Los diez mandamientos</replace>
   </wordEntry>
   <wordEntry>
      <search>The Texas Chainsaw Massacre</search>
      <replace>La matanza de Texas</replace>
   </wordEntry>
   <wordEntry>
      <search>The Three Musketeers</search>
      <replace>Los tres mosqueteros</replace>
   </wordEntry>
   <wordEntry>
      <search>The Trial</search>
      <replace>El proceso</replace>
   </wordEntry>
   <wordEntry>
      <search>The Untouchables</search>
      <replace>The Untouchables</replace>
   </wordEntry>
   <wordEntry>
      <search>Titanic</search>
      <replace>Titanic</replace>
   </wordEntry>
   <wordEntry>
      <search>To Kill a Mockingbird</search>
      <replace>Matar un ruiseñor</replace>
   </wordEntry>
   <wordEntry>
      <search>Treasure Island</search>
      <replace>La isla del tesoro</replace>
   </wordEntry>
   <wordEntry>
      <search>Ulysses</search>
      <replace>Ulises</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalism</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalism</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalism</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalism</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalist</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalist</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalist</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalist</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalists</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Universalists</search>
      <replace>Universalismo</replace>
   </wordEntry>
   <wordEntry>
      <search>Valais</search>
      <replace>Cantón del Valais</replace>
   </wordEntry>
   <wordEntry>
      <search>Valais</search>
      <replace>Cantón del Valais</replace>
   </wordEntry>
   <wordEntry>
      <search>Valencia</search>
      <replace>Comunidad Valenciana</replace>
   </wordEntry>
   <wordEntry>
      <search>Valencia</search>
      <replace>Provincia de Valencia</replace>
   </wordEntry>
   <wordEntry>
      <search>Valencia</search>
      <replace>Reino de Valencia</replace>
   </wordEntry>
   <wordEntry>
      <search>Valencia</search>
      <replace>Valencia</replace>
   </wordEntry>
   <wordEntry>
      <search>València</search>
      <replace>Valencia</replace>
   </wordEntry>
   <wordEntry>
      <search>València</search>
      <replace>Valencia</replace>
   </wordEntry>
   <wordEntry>
      <search>Vaudevillian</search>
      <replace>Vodevil</replace>
   </wordEntry>
   <wordEntry>
      <search>Vaudevillian</search>
      <replace>Vodevil</replace>
   </wordEntry>
   <wordEntry>
      <search>Vaudevillians</search>
      <replace>Vodevil</replace>
   </wordEntry>
   <wordEntry>
      <search>Vaudevillians</search>
      <replace>Vodevil</replace>
   </wordEntry>
   <wordEntry>
      <search>Volcano</search>
      <replace>Volcán</replace>
   </wordEntry>
   <wordEntry>
      <search>Volcano</search>
      <replace>Volcán</replace>
   </wordEntry>
   <wordEntry>
      <search>War and Peace</search>
      <replace>Guerra y paz</replace>
   </wordEntry>
   <wordEntry>
      <search>War of the Worlds</search>
      <replace>La guerra de los mundos</replace>
   </wordEntry>
   <wordEntry>
      <search>Witness for the Prosecution</search>
      <replace>Testigo de cargo</replace>
   </wordEntry>
   <wordEntry>
      <search>Wuthering Heights</search>
      <replace>Cumbres borrascosas</replace>
   </wordEntry>
   <wordEntry>
      <search>X chromosome</search>
      <replace>Cromosoma X</replace>
   </wordEntry>
   <wordEntry>
      <search>X chromosome</search>
      <replace>Cromosoma X</replace>
   </wordEntry>
   <wordEntry>
      <search>Xenophon</search>
      <replace>Jenofonte</replace>
   </wordEntry>
   <wordEntry>
      <search>Xenophon</search>
      <replace>Jenofonte</replace>
   </wordEntry>
   <wordEntry>
      <search>Yacht</search>
      <replace>Yate</replace>
   </wordEntry>
   <wordEntry>
      <search>Yacht</search>
      <replace>Yate</replace>
   </wordEntry>
   <wordEntry>
      <search>Yakuza</search>
      <replace>Yakuza</replace>
   </wordEntry>
   <wordEntry>
      <search>Yakuza</search>
      <replace>Yakuza</replace>
   </wordEntry>
   <wordEntry>
      <search>You Only Live Twice</search>
      <replace>Sólo se vive dos veces</replace>
   </wordEntry>
   <wordEntry>
      <search>Yugoslav Macedonia</search>
      <replace>República de Macedonia</replace>
   </wordEntry>
   <wordEntry>
      <search>Yugoslav Macedonia</search>
      <replace>República de Macedonia</replace>
   </wordEntry>
   <wordEntry>
      <search>Yugoslav Partisans</search>
      <replace>Partisanos</replace>
   </wordEntry>
   <wordEntry>
      <search>Yugoslav Partisans</search>
      <replace>Partisanos Yugoslavia</replace>
   </wordEntry>
   <wordEntry>
      <search>Zespri</search>
      <replace>Actinidia deliciosa</replace>
   </wordEntry>
   <wordEntry>
      <search>Zespri</search>
      <replace>Actinidia deliciosa</replace>
   </wordEntry>
   <wordEntry>
      <search>Zimniy Dvorets</search>
      <replace>Palacio de Invierno</replace>
   </wordEntry>
   <wordEntry>
      <search>Zimniy Dvorets</search>
      <replace>Palacio de Invierno</replace>
   </wordEntry>
   <wordEntry>
      <search>Zosimus</search>
      <replace>Zósimo</replace>
   </wordEntry>
   <wordEntry>
      <search>Zosimus</search>
      <replace>Zósimo historiador</replace>
   </wordEntry>
</wordlist>

Open in new window

0
AussieSilver
Asked:
AussieSilver
  • 7
  • 6
1 Solution
 
Geert BormansInformation ArchitectCommented:
you can use a second key
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="search" match="wordEntry" use="search"/>
    <xsl:key name="replace" match="wordEntry" use="concat(search, '-', replace)"/>
    <xsl:template match="wordlist">
        <Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="translation.xsd">
            <from><xsl:value-of select="@from" /></from>
            <to><xsl:value-of select="@to" /></to>
            <xsl:apply-templates select="wordEntry[generate-id() = generate-id(key('search', search)[1])]"/>
        </Dictionary>
    </xsl:template>
    <xsl:template match="wordEntry">
        
        <translation initial="{substring(search ,1,1)}">
            <xsl:variable name="this-search" select="search"></xsl:variable>
            <xsl:copy-of select="search"/>
            <counter>
                <xsl:value-of select="count(//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])])"/>
            </counter>
            <xsl:for-each select="//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])]">
                <xsl:copy-of select="replace"/>
            </xsl:for-each>
        </translation>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
AussieSilverAuthor Commented:
Hi,

The code works PERFECT !! but how about the <total> element?  I added the total element but I counl not count how may distinct search element? please read the question again and have a look at the amended XSLT code whereI added the <total> element but not working :(

Cheers,
0
 
AussieSilverAuthor Commented:
sorry

here is the code
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="search" match="wordEntry" use="search"/>
    <xsl:key name="replace" match="wordEntry" use="concat(search, '-', replace)"/>
    <xsl:template match="wordlist">
        <Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="translation.xsd">
            <from><xsl:value-of select="@from" /></from>
            <to><xsl:value-of select="@to" /></to>
			<total>
			<xsl:value-of select="wordEntry[generate-id() = generate-id(key('wordEntry', search)[1])]"/>
			</total>
            <xsl:apply-templates select="wordEntry[generate-id() = generate-id(key('search', search)[1])]"/>
        </Dictionary>
    </xsl:template>
    <xsl:template match="wordEntry">
        
        <translation initial="{substring(search ,1,1)}">
            <xsl:variable name="this-search" select="search"></xsl:variable>
            <xsl:copy-of select="search"/>
            <counter>
                <xsl:value-of select="count(//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])])"/>
            </counter>
            <xsl:for-each select="//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])]">
                <xsl:copy-of select="replace"/>
            </xsl:for-each>
        </translation>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Geert BormansInformation ArchitectCommented:
you forgot the count() function
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="search" match="wordEntry" use="search"/>
    <xsl:key name="replace" match="wordEntry" use="concat(search, '-', replace)"/>
    <xsl:template match="wordlist">
        <Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="translation.xsd">
            <from><xsl:value-of select="@from" /></from>
            <to><xsl:value-of select="@to" /></to>
            <total>
                <xsl:value-of select="count(wordEntry[generate-id() = generate-id(key('search', search)[1])])"/>
            </total>
            <xsl:apply-templates select="wordEntry[generate-id() = generate-id(key('search', search)[1])]"/>
        </Dictionary>
    </xsl:template>
    <xsl:template match="wordEntry">
        
        <translation initial="{substring(search ,1,1)}">
            <xsl:variable name="this-search" select="search"></xsl:variable>
            <xsl:copy-of select="search"/>
            <counter>
                <xsl:value-of select="count(//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])])"/>
            </counter>
            <xsl:for-each select="//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])]">
                <xsl:copy-of select="replace"/>
            </xsl:for-each>
        </translation>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
AussieSilverAuthor Commented:
YOU ARE REALLY GENIUS !!! can I put another thread to explain the XSLT code you have written in simple English? or you write them here? I really need to understand it

Cheers,
0
 
Geert BormansInformation ArchitectCommented:
The proper thing to do is to put it here.
Is it OK if I did that in an hour or so?
0
 
AussieSilverAuthor Commented:
I can wait for one hour.. after that I will accept your reply as the solution....
0
 
Geert BormansInformation ArchitectCommented:
Please find the explanation of the stylesheet in between the lines
note that I highly recommend that you read and understands Jeni's excellent article on Muenchian
Since this stylesheet goes beyond the complexity of that article, introducing compound keys
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" version="1.0" indent="yes"/>
===> serialises the output tree as XML v1.0, indented
    <xsl:strip-space elements="*"/>
===> strips all the white-space that could be considered redundant
    <xsl:key name="search" match="wordEntry" use="search"/>
===> this xsl:key indexes all wordEntry elements with the content of the search element as the indexing key
    <xsl:key name="replace" match="wordEntry" use="concat(search, '-', replace)"/>
===> this xsl:key indexes all wordEntry elements with the content of the search element, concatenated with the content of the replace element as the indexing key
    <xsl:template match="wordlist">
        <Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="translation.xsd">
            <from><xsl:value-of select="@from" /></from>
            <to><xsl:value-of select="@to" /></to>
            <total>
                <xsl:value-of select="count(wordEntry[generate-id() = generate-id(key('search', search)[1])])"/>
===> read article on Muenchian about this
===> http://www.jenitennison.com/xslt/grouping/muenchian.xml
===> instead of iterating using for-each, I just use the unique nodes in a count() for the total, count() counts the nodes
            </total>
            <xsl:apply-templates select="wordEntry[generate-id() = generate-id(key('search', search)[1])]"/>
===> again Muenchian, I push out all the unique nodes for evaluation by the templates ****
        </Dictionary>
    </xsl:template>
    <xsl:template match="wordEntry">
===> special template for wordEntry. This template will pick up all the nodes that are pushed out somewhere, and that will be elements with the name wordEntry
===> I push only the wordEntry element nodes out with a unique search value in ****
===> this template will only execute for unique search values
        <translation initial="{substring(search ,1,1)}">
            <xsl:variable name="this-search" select="search"></xsl:variable>
            <xsl:copy-of select="search"/>
            <counter>
                <xsl:value-of select="count(//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])])"/>
===> same count trick as before with <total> but using a compound key, finding unique values with a compound unique combination of search and replace
            </counter>
            <xsl:for-each select="//wordEntry[generate-id() =  generate-id(key('replace', concat($this-search, '-', replace))[1])]">
===> nested muenchian, same as before, but this time with a compound key
                <xsl:copy-of select="replace"/>
            </xsl:for-each>
        </translation>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
AussieSilverAuthor Commented:
sorry to open up this discussion again but I'm wondering why the total elements prints 150 instead 151? I have been informed that it should 151 but the result is 150 !!!

Cheers,
0
 
Geert BormansInformation ArchitectCommented:
strange,
if I count the number of translation elements in the output, i get 150 translation elements,
count(//translation) on the output
so based on the output, the count is right
I also tested on smaller sets (prior to posting the XSLT by the way) so I could manually count the input nodes and output nodes, and I did not miss one. So I was allready convinced about the correctness of the count prior to posting tyhe XSLT
It could be that I missed a record because of something in the data, but you can only know that if you start verifying the data

So, based on your follow up, I did this XPath test on the source data
count(//wordEntry[not(search = preceding-sibling::wordEntry/search)])
this is an expensive way to discover the unique values in a XML, with one single XPath 1.0
I also did this XPath2.0 test
count(distinct-values(//wordEntry/search))
With different XPath processors
All return 150

I am quiet convinced that the process counting 151 has a bug, or is counting a different result set.
(maybe also counting the header row in a table?)
0
 
AussieSilverAuthor Commented:
yeah... you are totally right... I believe that 151 is a bug...

Thanks mate...
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
 
jhonc66Commented:
Hi, this example is really helpful, one quick question,how can it be written using a for each loop instead of using the generate-id,just to see the differences...Thanks again
0
 
Geert BormansInformation ArchitectCommented:
hi jhonc66, I don't understand your question.

The first generate-id() is in an apply-templates, which is similar to for-each, but simply much better design
The second generate-id() is in a for-each

There is a way to find a ditinct node without muenchian
eg. to find the unique <bar>, based on a subelement <foo>
bar[not(foo = preceding-sibling::bar/foo)]

But that will imply looking up the entire preceding-sibling axis for every bar
and should be avoided in XSLT
It could help in a XPath only setting, but then I recommend moving to XPath2.0 and use the distinct-values() function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now