You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sugar/xml.rkt

25 lines
1.1 KiB
Racket

10 years ago
#lang racket/base
(require xml racket/port racket/contract "define.rkt")
10 years ago
(provide (all-defined-out))
(define+provide+safe (xml-string->xexprs str)
10 years ago
(string? . -> . (values xexpr? xexpr?))
9 years ago
(define xml-doc (with-input-from-string str
(λ () (permissive-xexprs #t) (read-xml))))
10 years ago
(values (xml->xexpr (document-prolog xml-doc)) (xml->xexpr (document-element xml-doc))))
9 years ago
(define+provide+safe (xexprs->xml-string prolog-xexpr root-xexpr)
10 years ago
(xexpr? xexpr? . -> . string?)
9 years ago
(with-output-to-string (λ () (write-xml (document (xexpr->xml prolog-xexpr) (xexpr->xml root-xexpr) null)))))
(module+ test
(require rackunit)
(require xml)
(define str "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>hello world</root>")
(define-values (str-prolog str-doc) (xml-string->xexprs str))
(check-equal? str-prolog (prolog (list (p-i (location 1 0 1) (location 1 38 39) 'xml "version=\"1.0\" encoding=\"utf-8\"")) #f null))
(check-equal? str-doc '(root () "hello world"))
9 years ago
(check-equal? (xexprs->xml-string str-prolog str-doc) str))