XSLT to HTML
-
Hi programmers, i have this task that has been troubling me due to my limited knowledge of XML/XSLT. My task is to create an XSLT(dictionary.xslt) file that will use an XML(translation.xml) file as input which in turn will generate a desired HTML(dictionary.html) file like the one below. - The index must be sorted alphabetically. This index serves as a Table Of Contents which allows users to browse through all 'initials' available in this dictionary. - For each 'initial', the correct number of 'search' terms must be returned. - I will be able to do the css styling but as for the xslt code and functions i have little clue, i'd appreciate if someone here with the knowledge will be able to help me with this. --- input XML (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="B">
<search>Batman & Robin</search>
<counter>1</counter>
<replace>Batman y Robin</replace>
</translation>
<translation initial="B">
<search>Bomarzo</search>
<counter>1< -
Hi programmers, i have this task that has been troubling me due to my limited knowledge of XML/XSLT. My task is to create an XSLT(dictionary.xslt) file that will use an XML(translation.xml) file as input which in turn will generate a desired HTML(dictionary.html) file like the one below. - The index must be sorted alphabetically. This index serves as a Table Of Contents which allows users to browse through all 'initials' available in this dictionary. - For each 'initial', the correct number of 'search' terms must be returned. - I will be able to do the css styling but as for the xslt code and functions i have little clue, i'd appreciate if someone here with the knowledge will be able to help me with this. --- input XML (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="B">
<search>Batman & Robin</search>
<counter>1</counter>
<replace>Batman y Robin</replace>
</translation>
<translation initial="B">
<search>Bomarzo</search>
<counter>1<This does most of what you want...I've put in comments to show the different bits...
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/><!-- We need a template matching the document root to start the transformation -->
<xsl:template match="/">
<html>
<head>
<title>Bilingual Lexicon ES-EN</title>
<link href="dictionary.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Browse by letter</h3>
<table>
<!-- Process each translation element, sorted by the initial attribute and then the English term -->
<xsl:apply-templates select="//translation">
<xsl:sort select="@initial"/>
<xsl:sort select="search"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template><!-- Template to process a translation element -->
<xsl:template match="translation">
<!--
If this is the first translation element, or its initial attribute is different
to the last one in the list of translation elements, output the
initial attribute.
-->
<xsl:if test="position()=1 or @initial != preceding-sibling::translation[1]/@initial">
<tr><td colspan="2"><h1><xsl:value-of select="@initial"/></h1></td></tr>
</xsl:if>
<!-- Output a row for each translation -->
<tr>
<td><xsl:value-of select="search"/></td>
<td>
<!-- Output the first replace element -->
<xsl:value-of select="replace[1]"/>
<!-- Output the other replace elements preceded by the separator -->
<xsl:for-each select="replace[position()>1]">
;<xsl:value-of select="."/>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!