AtlatestRepositorysigil-sxml
sigil-sxml / tree / docssxml.md
1
# SXML3
> SXML representation and HTML/XML serialization.5
```scheme6
(import (sigil sxml))7
```9
## SXML Format11
SXML is an S-expression representation of XML/HTML documents. Elements are lists starting with a tag symbol, with optional attributes in an `(@)` form.13
| Form | Description |14
|------|-------------|15
| `(tag content ...)` | Element with content |16
| `(tag (@ (attr val) ...) content ...)` | Element with attributes |17
| `"text"` | Text content |18
| `(*raw* "html")` | Raw HTML (no escaping) |19
| `(document content ...)` | Document wrapper (stripped on output) |21
```scheme22
;; Simple element23
'(p "Hello, world!")25
;; Element with attributes26
'(div (@ (class "main") (id "content")) "Hello")28
;; Nested elements29
'(ul (li "One") (li "Two") (li "Three"))30
```32
## sxml->html34
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.36
```scheme37
(sxml->html '(p "Hello"))38
; => "<p>Hello</p>"40
(sxml->html '(div (@ (class "main")) (p "text")))41
; => "<div class=\"main\"><p>text</p></div>"43
;; Void elements use HTML style (no closing slash)44
(sxml->html '(br))45
; => "<br>"47
(sxml->html '(img (@ (src "photo.jpg"))))48
; => "<img src=\"photo.jpg\">"50
;; Document wrapper is stripped51
(sxml->html '(document (p "From markdown")))52
; => "<p>From markdown</p>"53
```55
## sxml->xml57
Convert an SXML tree to an XML string. Void elements use self-closing syntax with ` />`.59
```scheme60
(sxml->xml '(item (@ (id "1")) "Content"))61
; => "<item id=\"1\">Content</item>"63
;; Void elements use XML style64
(sxml->xml '(img (@ (src "photo.jpg"))))65
; => "<img src=\"photo.jpg\" />"66
```68
## xml-escape70
Escape special XML characters (`&`, `<`, `>`, `"`, `'`) in text.72
```scheme73
(xml-escape "Tom & Jerry") ; => "Tom & Jerry"74
(xml-escape "<script>") ; => "<script>"75
(xml-escape "She said \"hi\"") ; => "She said "hi""76
```78
## Element Inspection80
Examine the structure of SXML elements.82
```scheme83
(sxml-element? '(div "hello")) ; => #t84
(sxml-element? "text") ; => #f86
(sxml-tag '(div (@ (class "box")) "content"))87
; => div89
(sxml-attributes '(div (@ (id "main") (class "box")) "text"))90
; => ((id "main") (class "box"))92
(sxml-attributes '(p "text"))93
; => ()95
(sxml-content '(div (@ (id "main")) "hello" " " "world"))96
; => ("hello" " " "world")97
```99
## Attribute Utilities101
Look up or modify attributes on SXML elements.103
```scheme104
;; Get attribute value105
(sxml-attr-ref '(div (@ (id "main"))) 'id) ; => "main"106
(sxml-attr-ref '(div (@ (id "main"))) 'class) ; => #f108
;; Set attribute (returns new element)109
(sxml-attr-set '(div "text") 'id "main")110
; => (div (@ (id "main")) "text")112
(sxml-attr-set '(div (@ (id "old"))) 'id "new")113
; => (div (@ (id "new")))114
```116
## Common Patterns118
### Building a Page120
```scheme121
(import (sigil sxml))123
(define (page title body)124
`(html125
(head (title ,title))126
(body ,@body)))128
(sxml->html129
(page "My Site"130
(list131
'(h1 "Welcome")132
'(p "Hello, world!"))))133
```135
### Raw HTML Content137
Use `*raw*` to embed pre-rendered HTML without escaping:139
```scheme140
(sxml->html141
`(div (@ (class "content"))142
(*raw* "<strong>Already formatted</strong>")))143
; => "<div class=\"content\"><strong>Already formatted</strong></div>"144
```146
### Inline Styles148
```scheme149
(sxml->html150
`(style (*raw* ,(css->string151
(css ".container" (max-width "800px"))))))152
```