sigildocs

(sigil sxml)

(sigil sxml) - SXML to XML/HTML Conversion

SXML is a standard S-expression representation of XML/HTML documents. This library converts SXML to XML or HTML strings for output.

SXML Format

FormDescription
(tag content ...)Element with content
(tag (@ (attr val) ...) content ...)Element with attributes
"text"Text content

Basic Usage

(import (sigil sxml))

;; HTML output (recommended for web pages)
(sxml->html '(html (head (title "Hello"))))
; => "<html><head><title>Hello</title></head></html>"

;; XML output (for XML documents)
(sxml->xml '(div (@ (class "main")) "Hello"))
; => "<div class=\"main\">Hello</div>"

Void Elements

Void elements like <br>, <img>, <input> are handled automatically. The output format differs between HTML and XML:

(sxml->html '(img (@ (src "photo.jpg"))))
; => "<img src=\"photo.jpg\">"

(sxml->xml '(img (@ (src "photo.jpg"))))
; => "<img src=\"photo.jpg\" />"

Exports

xml-escapeprocedure

Escape special XML characters in text.

Converts &, <, >, ", and ' to their XML entity equivalents.

(xml-escape "Tom & Jerry")  ; => "Tom &amp; Jerry"
(xml-escape "<script>")     ; => "&lt;script&gt;"
sxml-element?procedure

Check if a value is an SXML element.

An SXML element is a list starting with a symbol (the tag name).

(sxml-element? '(div "hello"))  ; => #t
(sxml-element? "text")          ; => #f
sxml-tagprocedure

Get the tag name from an SXML element.

(sxml-tag '(div (@ (class "box")) "content"))  ; => div

Check if second element is an attributes list

Get the attributes alist from an SXML element.

Returns an empty list if the element has no attributes.

(sxml-attributes '(div (@ (id "main") (class "box")) "text"))
; => ((id "main") (class "box"))

(sxml-attributes '(p "text"))
; => ()
sxml-contentprocedure

Get the content list from an SXML element.

Returns everything after the tag and optional attributes.

(sxml-content '(div (@ (id "main")) "hello" " " "world"))
; => ("hello" " " "world")
sxml-attr-refprocedure

Get an attribute value by name.

Returns #f if the attribute is not present.

(sxml-attr-ref '(div (@ (id "main"))) 'id)     ; => "main"
(sxml-attr-ref '(div (@ (id "main"))) 'class)  ; => #f
sxml-attr-setprocedure

Return a new element with an attribute added or updated.

Creates a new element; does not mutate the original.

(sxml-attr-set '(div "text") 'id "main")
; => (div (@ (id "main")) "text")

(sxml-attr-set '(div (@ (id "old"))) 'id "new")
; => (div (@ (id "new")))

Convert attribute list to XML string ((class "main") (id "content")) -> " class="main" id="content""

value->stringprocedure

Convert any value to string for output

List of void/self-closing elements (no closing tag needed)

void-element?procedure

Check if tag is a void element

List of raw text elements (content should not be escaped)

Check if tag is a raw text element

sxml->htmlprocedure

Convert an SXML tree to an HTML string.

Handles nested elements, attributes, text content, and void elements. Void elements like <br> and <img> are output without a closing slash. Content inside <style> and <script> tags is not escaped. A document wrapper element (from markdown->sxml) is stripped.

(sxml->html '(p "Hello"))
; => "<p>Hello</p>"

(sxml->html '(div (@ (class "main")) (p "text")))
; => "<div class=\"main\"><p>text</p></div>"

(sxml->html '(img (@ (src "foo.png"))))
; => "<img src=\"foo.png\">"

(sxml->html '(document (p "From markdown")))
; => "<p>From markdown</p>"
sxml->xmlprocedure

Convert an SXML tree to an XML string.

Handles nested elements, attributes, text content, and void elements. Void elements are self-closing with /> (XML style). Content inside <style> and <script> tags is not escaped.

(sxml->xml '(p "Hello"))
; => "<p>Hello</p>"

(sxml->xml '(div (@ (class "main")) (p "text")))
; => "<div class=\"main\"><p>text</p></div>"

(sxml->xml '(img (@ (src "foo.png"))))
; => "<img src=\"foo.png\" />"