On this page:
srl:  sxml->xml
srl:  sxml->xml-noindent
srl:  sxml->html
srl:  sxml->html-noindent

3 Serialization

procedure

(srl:sxml->xml sxml-obj)  string?

  sxml-obj : sxml?
(srl:sxml->xml sxml-obj dest)  void?
  sxml-obj : sxml?
  dest : (or/c output-port? path-string?)
Serializes the SXML node or nodeset sxml-obj into XML, with indentation to facilitate readability by a human.

If dest is not supplied, the function returns a string that contains the serialized representation of the sxml-obj. If dest is supplied and is a port, the functions write the serialized representation of sxml-obj to this port. If dest is supplied and is a string, this string is treated as an output filename, the serialized representation of sxml-obj is written to that filename. If a file with the given name already exists, the effect is unspecified.

Examples:
> (srl:sxml->xml '(zippy (pippy (@ (pigtails "2")) "ab") "bc"))

"<zippy><pippy pigtails=\"2\">ab</pippy>bc</zippy>"

> (srl:sxml->xml '(zippy (pippy (@ (pigtails "2")) "ab") "bc")
                 (current-output-port))

<zippy><pippy pigtails="2">ab</pippy>bc</zippy>

> (srl:sxml->xml (for/fold ([body '(nothing)]) ([i (in-range 5)])
                   `(doll (@ (level ,(number->string i))) ,body))
                 (current-output-port))

<doll level="4">

  <doll level="3">

    <doll level="2">

      <doll level="1">

        <doll level="0">

          <nothing />

        </doll>

      </doll>

    </doll>

  </doll>

</doll>

procedure

(srl:sxml->xml-noindent sxml-obj)  string?

  sxml-obj : sxml?
(srl:sxml->xml-noindent sxml-obj dest)  void?
  sxml-obj : sxml?
  dest : (or/c output-port? path-string?)
Like srl:sxml->xml but without indentation.

Examples:
> (srl:sxml->xml-noindent
   '(zippy (pippy (@ (pigtails "2")) "ab") "bc"))

"<zippy><pippy pigtails=\"2\">ab</pippy>bc</zippy>"

> (srl:sxml->xml-noindent
   '(zippy (pippy (@ (pigtails "2")) "ab") "bc")
   (current-output-port))

<zippy><pippy pigtails="2">ab</pippy>bc</zippy>

> (srl:sxml->xml-noindent
   (for/fold ([body '(nothing)]) ([i (in-range 5)])
     `(doll (@ (level ,(number->string i))) ,body))
   (current-output-port))

<doll level="4"><doll level="3"><doll level="2"><doll level="1"><doll level="0"><nothing /></doll></doll></doll></doll></doll>

procedure

(srl:sxml->html sxml-obj)  string?

  sxml-obj : sxml?
(srl:sxml->html sxml-obj dest)  void?
  sxml-obj : sxml?
  dest : (or/c output-port? path-string?)
Serializes the SXML node or nodeset sxml-obj into HTML, with indentation to facilitate readability by a human.

If dest is not supplied, the functions return a string that contains the serialized representation of the sxml-obj. If dest is supplied and is a port, the functions write the serialized representation of sxml-obj to this port. If dest is supplied and is a string, this string is treated as an output filename, the serialized representation of sxml-obj is written to that filename. If a file with the given name already exists, the effect is unspecified.

NOTE: As far as I can tell, the result of this transformation is more accurately described as XHTML than as HTML. The most noticeable difference is that certain tags may be rendered surprisingly by many browsers when they are expressed in the <tag /> form rather than in the <tag></tag> form. Giving these tags an empty string as a body will force them to render correctly. The list of tags for which the W3 consortium recommends using the expanded form is this:
  • param

  • meta

  • link

  • isindex

  • input

  • img

  • hr

  • frame

  • col

  • br

  • basefont

  • base

  • area

Should this function automatically treat these differently? Yes, probably so.

procedure

(srl:sxml->html-noindent sxml-obj)  string?

  sxml-obj : sxml?
(srl:sxml->html-noindent sxml-obj dest)  void?
  sxml-obj : sxml?
  dest : (or/c output-port? path-string?)
Like srl:sxml->html but without indentation.