XML to XML with XSLT

Here should go questions about transforming XML with XSLT and FOP.
Trx
Posts: 1
Joined: Wed Jun 26, 2024 1:30 pm

XML to XML with XSLT

Post by Trx »

Hi,
How do I XSL-transform two XML files containing words in two different languages and the result of the transformation will be a lexicon?
I have two XML files and each contains 10 words and SVG file which will be the logo of the lexicon. I need some help with the XSL file as it is not displaying any XML content from the files. Would appreciate if someone could take a look on this.

XML file 1: 10 Swedish words

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="wordlist_style.css"?>
<wordlist>
    <language>Svenska</language>
    <author>Karl Jonsson</author>
    <words>
        <word>natt</word>
        <word>dag</word>
        <word>vatten</word>
        <word>bil</word>
        <word>sol</word>
        <word>himmel</word>
        <word>stad</word>
        <word>hus</word>
        <word>katt</word>
        <word>regn</word>
    </words>
</wordlist>
XML file 2: 10 English words

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<wordlist>
   <language>English</language>
   <author>David Cahill</author>
   <words>
      <word>night</word>
      <word>day</word>
      <word>water</word>
      <word>car</word>
      <word>sun</word>
      <word>sky</word>
      <word>city</word>
      <word>house</word>
      <word>cat</word>
      <word>rain</word>
   </words>
</wordlist>
XML file that has paths to the two above XML files and a SVG file

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="lexicon.xsl"?>
<dictionary>
    <dictionaryLink>wordlist_english.xml</dictionaryLink>
    <dictionaryLink>wordlist_swedish.xml</dictionaryLink>
    <svgLogoLink>logo.svg</svgLogoLink>
</dictionary>
XSL file that will transform the XML files to a lexicon

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="dic1">
        <xsl:value-of select="dictionary/dictionaryLink[1]"/>
    </xsl:variable>
    <xsl:variable name="dic2">
        <xsl:value-of select="dictionary/dictionaryLink[2]"/>
    </xsl:variable>
    <xsl:variable name="logo">
        <xsl:value-of select="dictionary/svgLogoLink"/>
    </xsl:variable>
    <xsl:template match="/dictionary">
        <html>
            <body>
                <xsl:value-of select="document($dic1)/wordlist/@xml:lang" />
                <br/>
                    <img src="{lexicon/logo}"/>
                    <h1>Swedish - English</h1>
                    <div class="left">
                        <h2>Swedish - English</h2>
                        <ul>
                            <xsl:for-each select="wordlist/word">
                                <li><xsl:value-of select="words"/></li>
                            </xsl:for-each>
                        </ul>
                    </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
tavy
Posts: 387
Joined: Thu Jul 01, 2004 12:29 pm

Re: XML to XML with XSLT

Post by tavy »

Hello,

Firstly, the path for the words in the XSLT template is incorrect. Also, you would have to open two documents itself, one for each language. Here is an improved XSLT:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" version="2.0" encoding="UTF-8"/>
    <xsl:template match="/dictionary">
        <xsl:variable name="dic1" select="dictionaryLink[1]"/>
        <xsl:variable name="dic2" select="dictionaryLink[2]"/>
        <xsl:variable name="logo" select="svgLogoLink"/>
        
        <html>
            <body>
                <img src="{$logo}" />
                <h1>Swedish - English</h1>
                <div class="left">
                    <h2>Swedish Words</h2>
                    <ul>
                        <xsl:for-each select="document($dic1)/wordlist/words/word">
                            <li><xsl:value-of select="."/></li>
                        </xsl:for-each>
                    </ul>
                    <h2>English Words</h2>
                    <ul>
                        <xsl:for-each select="document($dic2)/wordlist/words/word">
                            <li><xsl:value-of select="."/></li>
                        </xsl:for-each>
                    </ul>
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
The for-each loop now selects each word of the wordlists for the two languages from the respective files indicated by the "dic1" and "dic2" variables. The select part of each "for-each" queries the opening of the document which is indicated by the variable.

Also, fixing the 'img' src should now properly display your logo, where the path to the logo is stored in the 'logo' variable.

Make sure to place all the XML files and the XSL file in the same directory to avoid possible issues with relative paths.

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply