Hello World (XSL)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.


XSLT transformations permit us to add extra layers to source data.

Using mode="text output"

<<hello-world-text.xsl>>=
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:template match="/">
                <xsl:apply-templates mode="passone"/>
                <xsl:apply-templates mode="passtwo"/>
                <xsl:call-template name="recurse">
                        <xsl:with-param name="num">
                                <xsl:value-of select="1"/>
                        </xsl:with-param>
                </xsl:call-template>
        </xsl:template>
        <xsl:template match="exchange" mode="passone">
                <xsl:text>Bugs says: </xsl:text><xsl:value-of select="greeting" /><xsl:text>
</xsl:text>
        </xsl:template>
        <xsl:template match="exchange" mode="passtwo">
                <xsl:text>Daffy says: </xsl:text><xsl:value-of select="response" /><xsl:text>
</xsl:text>
        </xsl:template>
        <xsl:template name="recurse">
                <xsl:param name="num">1</xsl:param>
                <xsl:if test="not ($num = 11)">
                        <xsl:text><xsl:value-of select="$num" />
</xsl:text>
                        <xsl:call-template name="recurse">
                                <xsl:with-param name="num">
                                        <xsl:value-of select="$num + 1"/>
                                </xsl:with-param>
                        </xsl:call-template>
                </xsl:if>
        </xsl:template>
</xsl:stylesheet>
<<hello-world-text.xml>>=
<exchange>
  <greeting>What's up, doc?</greeting>
  <response>Oooooo, you're despicable!</response>
</exchange>

The xml file and the xsl translation produce the following output:

Bugs says: What's up, doc?
Daffy says: Oooooo, you're despicable!
1
2
3
4
5
6
7
8
9
10

Using a web browser to generate XHTML

You specify the xsl stylesheet in the xml-stylesheet Processing Instruction.

<<hello-world.xml>>=
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="hello-world.xsl" type="text/xsl" ?>
<doc>
   <title>Hello World Test</title>
   <hello>World</hello>
</doc>

You should be careful with namespace definitions, or it will not work on Mozilla.

<<hello-world.xsl>>=
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output 
    method="xml"
    encoding="iso-8859-1"
    indent="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  />
  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="doc/title"/></title>
      </head>
      <body>
        <p>Hello <xsl:value-of select="doc/hello"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

With the two files on the same directory you open the xml file with the browser or call its URL if it is in a web zone.

The browser transforms the source nodes with the xsl templates acting as if it had received the following result:

<<hello-world.html>>=
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Hello World Test</title>
  </head>
  <body>
    <p>Hello World</p>
  </body>
</html>

With "Literal Result" XSL

For simple transformations (only one template: root) you can use the "Literal Result Element" style which permits to have the standalone template contents in a file that looks like having the result format with xsl embedded instructions.

<<hello-world-lr.xsl>>=
<html xsl:version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title><xsl:value-of select="doc/title"/></title>
  </head>
  <body>
    <p>Hello <xsl:value-of select="doc/hello"/></p>
  </body>
</html>

Substitute the stylesheet filename in the source xml file and retry to see the result of the literal result xsl transformation.

Download code
Views