My task is to create a translation.xslt file that will use the wordlist.xml as input to transform into another XML file(translation.xml). I have attempted some coding but stuck how to do the rest. I am not sure how to create a <total>, he value of <total> is the total number of unique search terms available in the
dictionary, so it should give me 150 like the sample. I also need to add in a counter element that will group and display the value of how many <replaces> there are when someone searches a word.

---translation.xslt document----

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>

<xsl:variable name="smallcase" select="'azertyuiopqsdfghjklmwxcvbn'"></xsl:variable>
<xsl:variable name="uppercase" select="'AZERTYUIOPQSDFGHJKLMWXCVBN'"></xsl:variable>

<xsl:key name="search" match="wordEntry" use="search"/>
<xsl:key name="replace" match="wordEntry" use="replace"/>

<xsl:template match="wordlist">
<Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="translation.xsd">
<from><xsl:value-of select="translate(@from, $smallcase, $uppercase)"/></from>
<to><xsl:value-of select="translate(@to, $smallcase, $uppercase)"/></to>
<xsl:apply-templates/>
</Dictionary>
</xsl:template>

<xsl:template match="wordEntry">
<translation initial="{substring(search ,1,1)}">
<xsl:element name="search">
<xsl:value-of select="search"/>
</xsl:element>
<counter>
</counter>
<xsl:element name="replace">
<xsl:value-of select="replace"/>
</xsl:element>
</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 & 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>
etc...
</wordlist>

---**output of what it should look like (translation.xml)**---

<?xml version="1.0" encoding="UTF-8"?>
<Dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="translation.xsd">
<from>EN</from>
<to>ES</to>
<total>150</total>
<translation initial="A">
<search>A Clockwork Orange</search>
<counter>1</counter>
<replace>La naranja mecánica</replace>
</translation>
<translation initial="A">
<search>A Few Good Men</search>
<counter>1</counter>
<replace>A Few Good Men</replace>
</translation>
<translation initial="A">
<search>A Star Is Born</search>
<counter>1</counter>
<replace>Ha nacido una estrella</replace>
</translation>
<translation initial="A">
<search>Ab Urbe condita</search>
<counter>1</counter>
<replace>Ab Urbe condita libri</replace>
</translation>
<translation initial="A">
<search>Ab urbe condita</search>
<counter>1</counter>
<replace>Ab urbe condita</replace>
</translation>
<translation initial="A">
<search>Abel</search>
<counter>2</counter>
<replace>Abel</replace>
<replace>Caín</replace>
</translation>
<translation initial="A">
<search>Alpinism</search>
<counter>1</counter>
<replace>Montañismo</replace>
</translation>
<translation initial="A">
<search>Alyosha Karamazov</search>
<counter>1</counter>
<replace>Los hermanos Karamazov</replace>
</translation>
<translation initial="A">
<search>And Then There Were None</search>
<counter>1</counter>
<replace>Diez negritos</replace>
</translation>
<translation initial="A">
<search>Angst</search>
<counter>1</counter>
<replace>Gaslight</replace>
</translation>
<translation initial="A">
<search>Around The World In 80 Days</search>
<counter>1</counter>
<replace>La vuelta al mundo en ochenta días</replace>
</translation>
<translation initial="A">
<search>Around the World in Eighty Days</search>
<counter>1</counter>
<replace>La vuelta al mundo en ochenta días</replace>
</translation>
<translation initial="B">
<search>Batman & Robin</search>
<counter>1</counter>
<replace>Batman y Robin</replace>
</translation>
<translation initial="B">
<search>Bomarzo</search>
<counter>1</counter>
<replace>Bomarzo</replace>
</translation>
<translation initial="B">
<search>Brave New World</search>
<counter>1</counter>
<replace>Un mundo feliz</replace>
</translation>
etc.....
</Dictionary>

----------------------------------------------

Thanks in advance

Edited 6 Years Ago by vho123: n/a

Here is the solution of your problem: (I just tweak you xslt)
But one condition that the xml node hierarchy should be same as given in your xml.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="smallcase" select="'azertyuiopqsdfghjklmwxcvbn'"></xsl:variable>
  <xsl:variable name="uppercase" select="'AZERTYUIOPQSDFGHJKLMWXCVBN'"></xsl:variable>

  <xsl:template match="/">
    <xsl:call-template name="DictionaryNode"></xsl:call-template>
  </xsl:template>

  <xsl:template name="DictionaryNode">
    <Dictionary>
      <from>
        <xsl:value-of select="translate(wordlist/@from, $smallcase, $uppercase)"/>
      </from>
      <to>
        <xsl:value-of select="translate(wordlist/@to, $smallcase, $uppercase)"/>
      </to>
      <xsl:call-template name="intekhab">
      </xsl:call-template>
    </Dictionary>
  </xsl:template>

  <xsl:template name="intekhab">
    <xsl:variable name="searchNodes" select="//preceding::search/preceding::search[not(.=preceding::search)]"/>
    <xsl:for-each select="$searchNodes">
      <xsl:sort select="."/>
      <translation initial="{substring(. ,1,1)}">
        <xsl:element name="search">
          <xsl:value-of select="."/>
        </xsl:element>
        <xsl:variable name="curSearch" select="."/>
        <xsl:variable name="totalSearch" select="//search[text()=$curSearch]"/>
        <xsl:variable name ="countTotalSearch" select="count($totalSearch)"/>
        <counter>
          <xsl:value-of select="$countTotalSearch"/>
        </counter>
        <xsl:element name="replace">
          <xsl:value-of select="following-sibling::replace"/>
        </xsl:element>
      </translation>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

for total unique replacement:
add second expression after the first
<xsl:variable name="searchNodes" select="//preceding::search/preceding::search[not(.=preceding::search)]"/>
<xsl:variable name="totalSearchReplaceCount" select="count($searchNodes)"/>

to display in xml add this line
<TotalReplcament>
<xsl:value-of select="$totalSearchReplaceCount"/>
</TotalReplcament>

This article has been dead for over six months. Start a new discussion instead.