6.3. SXML to XML

This example

Example 14. XSieve program sxml2xml.xsl

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

<s:init>
(define read-rest             2
  (lambda ()
    (let ((obj (read)))
      (if (eof-object? obj)
        '()
        (cons obj (read-rest))))))
</s:init>

<x:template match="/">        3
  <s:scheme>
    (with-input-from-file (x:eval "$input") read-rest)  45
  </s:scheme>
</x:template>

</x:stylesheet>
1

The parameter input is used to pass the name of an input SXML file to the program.

2

The recursive function read-rest reads all S-expressions from the default input port and returns them in a list. Actually, a good SXML file contains only one S-expression which starts with the symbol *TOP*. So, for a good SXML file, it's enough to use the function read only once. Anyway, sometimes it is useful to process not-so-good SXML files.

3

The name of an input SXML file is passed as a parameter, so an input XML file is not important and it is used only to activate the template.

4

Result of the function x:eval is the name of an input SXML file.

5

The function with-input-from-file opens an input SXML file, temporary sets it as the default input port and runs the function read-rest.

Example 15. Running sxml2xml.xsl

$ xsieve --stringparam input SXML-file sxml2xml.xsl sxml2xml.xsl