6.2. Hello, World II, or XML to SXML

This example demonstrates automatic conversion of data from XML to the SXML format. Additionally, it's a way to convert an XML file to the SXML format.

Example 11. XML to SXML stylesheet

<x:stylesheet
  xmlns:x = "http://www.w3.org/1999/XSL/Transform"
  xmlns:s = "http://xsieve.sourceforge.net"
  extension-element-prefixes="s"
  version = "1.0">
<!-- -->

<s:init>
  (use-modules (ice-9 pretty-print))   1
</s:init>

<x:template match="/">                 2
  <s:scheme>
    (pretty-print (x:current))         3
  </s:scheme>
</x:template>

</x:stylesheet>
1

Scheme code inside s:init is executed before applying templates. In this example, the code loads a Guile module that supports pretty printing.

2

The only one XSLT template, which matches the root node of an XML file.

3

The XSieve function x:current returns the context node of a template, transparently converting the node from XML to SXML. In this template, the context node is the root node.

The pretty-print function puts the pretty printed SXML result into the standard output. SXML is the only point of interest in this example, so the result of the XML transformation can be ignored. Thereby, one can execute the stylesheet the following way.

$ xsieve -o /dev/null hello2.xsl XML-file

Let XML-file be the following file.

Example 12. XML file for the XML to SXML example

<?xml version="1.0"?>
<article id="hw">
  <title>Hello</title>
  <para>Hello, <object>World</object>!</para>
</article>

The output of the stylesheet for the XML file is the following.

Example 13. SXML result for the XML to SXML example

(*TOP* (article            1
         (@ (id "hw"))
         "                 2
  "
         (title "Hello")
         "
  "
         (para "Hello, " (object "World") "!")
         "
"))
1

The symbol *TOP* stands for the document root node, and article is the document top-level element node.

2

Space between tags is character data. In this case it consist of the new line symbol followed by two space symbols.