1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

XSLT to XML transformation

Discussion in 'Web Design & Development' started by vho, Apr 28, 2010.

Thread Status:
Not open for further replies.
  1. vho

    vho Thread Starter

    Joined:
    Apr 28, 2010
    Messages:
    1
    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> that will count all the distinct nodes which should give a value of 150 as shown in the sample output. I also need to add in a counter element that will give the value of how many <replaces> there are when someone searches a word. I seem to also be getting duplicates because of it i think.

    ---translation.xslt document----
    Code:
    <?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
    Code:
    <?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>
    <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>
    etc...
    </wordlist>
    
    ---**output of what it should look like (translation.xml)**---
    Code:
    <?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
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/919886

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice