954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XSLT to XML transformation

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 , he value of 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 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

vho123
Newbie Poster
4 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

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"?>

Intekhab_Sheikh
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

for total unique replacement:
add second expression after the first

to display in xml add this line

Intekhab_Sheikh
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

hope you got the answer!!

Intekhab_Sheikh
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: