QifshaLopen
QifshaLopen

Reputation: 15

XML to XHTML with XSL

I have two XML files containing two dictionaries with 10 words in two different languages (same words). I want now to use XSL to link these two XMLs and transform them to XHTML.

The way i do it now, it only seem to give me a HTML output. What do I do, do I have to transform the HTML into XHTML or is it a way to transform it to XHTML directly?

This is how one of my dictionary XML look like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="dict.css"?> 
<Dictionary xmlns="https://translate.google.se/m/translate?hl=sv/german">
    <Language>Swedish</Language>
    <Content>
        <Titel>Svensk ordlista</Titel>
        <Author>
            <Name> Translator </Name>
        </Author>
        <Words wordNum ="10">
            <Word ID="0">Vatten</Word>
            <Word ID="1">Häst</Word>
            <Word ID="2">Bil</Word>
            <Word ID="3">Katt</Word>
            <Word ID="4">Hund</Word>
            <Word ID="5">Snö</Word>
            <Word ID="6">Gata</Word>
            <Word ID="7">Hus</Word>
            <Word ID="8">Bord</Word>
            <Word ID="9">Hand</Word>
        </Words>
    </Content>
</Dictionary>

This is how my XML linking look like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="dictionary.xsl"?>
<links>
    <dictLink>german.xml</dictLink>
    <dictLink>Dic-swedish.xml</dictLink>
    <svgLogo>svglogo.svg</svgLogo>
</links>

And this is how my XSL looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:variable name="dictionary1">
<xsl:value-of select="/links/dictLink[1]" />
</xsl:variable>

<xsl:variable name="dictionary2">
    <xsl:value-of select="/dictionaries/dictLink[2]" />
</xsl:variable>

<xsl:variable name="logo">
    <xsl:value-of select="/dictionaries/svgLogo" />
</xsl:variable>

<xsl:template match="/dictionaries">

<html> <body> <xsl:value-of select="document($dic1)/Dictionary/@xml:Language"/><br/> </xsl:for-each> </body></html> </xsl:template> </xsl:stylesheet>

Upvotes: 0

Views: 3350

Answers (2)

Tony Graham
Tony Graham

Reputation: 8068

Since you're using XSLT 1.0, see https://www.w3.org/TR/1999/REC-xslt-19991116#output.

Because the document element in your result tree is html in no namespace, you are getting HTML output.

See https://www.w3.org/TR/1999/REC-xslt-19991116#section-Document-Example for an XSLT 1.0 example of producing XHTML output. The key parts from that are:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/TR/xhtml1/strict">

<xsl:output
   method="xml"
   indent="yes"
   encoding="iso-8859-1"
/>

If you want the DOCTYPE declaration, you could change your xsl:output to:

<xsl:output
    method="xml"
    indent="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    omit-xml-declaration="yes" />

Personally, I wouldn't bother setting the encoding to ISO-8859-1. Without the encoding attribute, you will get UTF-8 or UTF-16, and it's more than likely that it will be UTF-8. Whether it is UTF-8 or UTF-16, the XML system that uses the output will be able to handle the encoding.

Upvotes: 1

zx485
zx485

Reputation: 29022

This is probably only a partial solution, but you can use the following templates to output some (X)HTML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" 
xmlns:de="https://translate.google.se/m/translate?hl=sv/german" 
exclude-result-prefixes="fo de">
  <xsl:output method="html" indent="yes" />

  <xsl:template match="/links">
    <html>
        <body> 
            <xsl:for-each select="dictLink">
                <xsl:copy-of select="document(.)/*[local-name()='Dictionary']" /><br/> 
            </xsl:for-each>
        </body>
    </html> 
  </xsl:template> 
</xsl:stylesheet>

If you name this XSLT dictionary.xsl and call the second XML from the browser, it should copy the contents of Dic-swedish.xml (the first XML) to the output.

Upvotes: 0

Related Questions