SXML
SXML representation and HTML/XML serialization.
(import (sigil sxml))SXML Format
SXML is an S-expression representation of XML/HTML documents. Elements are lists starting with a tag symbol, with optional attributes in an (@) form.
| Form | Description |
|---|---|
(tag content ...) | Element with content |
(tag (@ (attr val) ...) content ...) | Element with attributes |
"text" | Text content |
(*raw* "html") | Raw HTML (no escaping) |
(document content ...) | Document wrapper (stripped on output) |
;; Simple element
'(p "Hello, world!")
;; Element with attributes
'(div (@ (class "main") (id "content")) "Hello")
;; Nested elements
'(ul (li "One") (li "Two") (li "Three"))sxml->html
Convert an SXML tree to an HTML string. Void elements like <br> and <img> are output without a closing slash. Content inside <style> and <script> tags is not escaped.
(sxml->html '(p "Hello"))
; => "<p>Hello</p>"
(sxml->html '(div (@ (class "main")) (p "text")))
; => "<div class=\"main\"><p>text</p></div>"
;; Void elements use HTML style (no closing slash)
(sxml->html '(br))
; => "<br>"
(sxml->html '(img (@ (src "photo.jpg"))))
; => "<img src=\"photo.jpg\">"
;; Document wrapper is stripped
(sxml->html '(document (p "From markdown")))
; => "<p>From markdown</p>"sxml->xml
Convert an SXML tree to an XML string. Void elements use self-closing syntax with />.
(sxml->xml '(item (@ (id "1")) "Content"))
; => "<item id=\"1\">Content</item>"
;; Void elements use XML style
(sxml->xml '(img (@ (src "photo.jpg"))))
; => "<img src=\"photo.jpg\" />"xml-escape
Escape special XML characters (&, <, >, ", ') in text.
(xml-escape "Tom & Jerry") ; => "Tom & Jerry"
(xml-escape "<script>") ; => "<script>"
(xml-escape "She said \"hi\"") ; => "She said "hi""Element Inspection
Examine the structure of SXML elements.
(sxml-element? '(div "hello")) ; => #t
(sxml-element? "text") ; => #f
(sxml-tag '(div (@ (class "box")) "content"))
; => div
(sxml-attributes '(div (@ (id "main") (class "box")) "text"))
; => ((id "main") (class "box"))
(sxml-attributes '(p "text"))
; => ()
(sxml-content '(div (@ (id "main")) "hello" " " "world"))
; => ("hello" " " "world")Attribute Utilities
Look up or modify attributes on SXML elements.
;; Get attribute value
(sxml-attr-ref '(div (@ (id "main"))) 'id) ; => "main"
(sxml-attr-ref '(div (@ (id "main"))) 'class) ; => #f
;; Set attribute (returns new element)
(sxml-attr-set '(div "text") 'id "main")
; => (div (@ (id "main")) "text")
(sxml-attr-set '(div (@ (id "old"))) 'id "new")
; => (div (@ (id "new")))Common Patterns
Building a Page
(import (sigil sxml))
(define (page title body)
`(html
(head (title ,title))
(body ,@body)))
(sxml->html
(page "My Site"
(list
'(h1 "Welcome")
'(p "Hello, world!"))))Raw HTML Content
Use *raw* to embed pre-rendered HTML without escaping:
(sxml->html
`(div (@ (class "content"))
(*raw* "<strong>Already formatted</strong>")))
; => "<div class=\"content\"><strong>Already formatted</strong></div>"Inline Styles
(sxml->html
`(style (*raw* ,(css->string
(css ".container" (max-width "800px"))))))