(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
| Form | Description |
|---|---|
(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-escapeprocedureEscape special XML characters in text.
Converts &, <, >, ", and ' to their XML entity equivalents.
(xml-escape "Tom & Jerry") ; => "Tom & Jerry"
(xml-escape "<script>") ; => "<script>"sxml-element?procedureCheck 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") ; => #fsxml-tagprocedureGet the tag name from an SXML element.
(sxml-tag '(div (@ (class "box")) "content")) ; => divhas-attributes?procedureCheck if second element is an attributes list
sxml-attributesprocedureGet 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-contentprocedureGet 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-refprocedureGet 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) ; => #fsxml-attr-setprocedureReturn 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")))attributes->xmlprocedureConvert attribute list to XML string ((class "main") (id "content")) -> " class="main" id="content""
value->stringprocedureConvert any value to string for output
void-elementsvariableList of void/self-closing elements (no closing tag needed)
void-element?procedureCheck if tag is a void element
raw-text-elementsvariableList of raw text elements (content should not be escaped)
raw-text-element?procedureCheck if tag is a raw text element
sxml->htmlprocedureConvert 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->xmlprocedureConvert 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\" />"