[plt-scheme] SXML Experience?
Thanks for you answer, In between, i've build this.
It's not very good, but functional. It transforms and has
(not intended) side effects.
(define (xml-get xml xpath default)
(let ((v ((sxpath xpath) xml)))
(if (null? v)
default
(cadr v))))
(define (xml-set xml xpath value)
(define (create-branch xml xpath element)
(if (and (null? (cdr xpath)) (eq? (car xpath) '//))
(if (null? ((sxpath xpath) xml))
'(*TOP* (*PI* xml "version=\"1.0\" encoding=\"UTF-8\"") (element
'nil))
xml)
(let ((v ((sxpath xpath) xml)))
(if (null? v)
(let* ((rxpath (reverse xpath))
(nelem (car rxpath))
(nxpath (reverse (cdr rxpath))))
(let ((nxml (create-branch xml nxpath nelem)))
(create-branch nxml xpath element)))
(let ((nv (car v)))
(if (eq? (car (cdr nv)) 'sxml-nil)
(set-cdr! nv (list (list (string->symbol element) 'sxml-nil)))
(set-cdr! nv (cons (list (string->symbol element) 'sxml-nil)
(cdr nv))))
xml)))))
(let ((v ((sxpath xpath) xml)))
(if (null? v)
(let* ((rxpath (reverse xpath))
(nelem (car rxpath))
(nxpath (reverse (cdr rxpath))))
(let ((nxml (create-branch xml nxpath nelem)))
(xml-set nxml xpath value)))
(begin
(set-car! (cdar v) value)
xml))))