6.3. SXML to XML

This example

Example 14. XSieve program sxml2xml.xsl

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

(define read-rest             2
  (lambda ()
    (let ((obj (read)))
      (if (eof-object? obj)
        (cons obj (read-rest))))))

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


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


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.


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.


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


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