;;; $Id: remove-named-mod.scm,v 1.1 2005/12/30 18:11:06 leavens Exp $ ;;; AUTHOR: Gary T. Leavens, with help from the students in Com S 342 ;;; ;;; Remove all elements with a given name (including everything ;;; underneath), assuming that that the element doesn't occur as the ;;; top element in the document. (module remove-named-mod (lib "typedscm.ss" "typedscm") (provide remove-named) (require (lib "sxml-helpers.scm" "lib342")) (deftype remove-named (-> (document symbol) document)) (define remove-named (lambda (doc nm) ;; ENSURES: result is like doc except that any elements under the ;; top element with name nm are not present. (document (document->PIs doc) (document->comments doc) (remove-named-elem (document->element doc) nm)))) (deftype remove-named-elem (-> (element symbol) element)) (define remove-named-elem (lambda (elem nm) ;; ENSURES: result is like elem except that any child elements ;; with name nm are not present. (element (element->name elem) (element->attributes elem) (filter-out-named (element->children elem) nm)))) (deftype filter-out-named (-> ((list-of child-of-element) symbol) (list-of child-of-element))) (define filter-out-named (lambda (locoe nm) ;; ENSURES: result is like locoe, except that any child elements ;; with name nm are not present. (if (null? locoe) '() (append (filter-out-child (car locoe) nm) (filter-out-named (cdr locoe) nm))))) (deftype filter-out-child (-> (child-of-element symbol) (list-of child-of-element))) (define filter-out-child (lambda (coe nm) ;; ENSURES: if the element is a child with name nm, return (), ;; otherwise return a list containing coe, with any child elements ;; with name nm not present. (cond ((child? coe) (if (eq? nm (element->name (child->element coe))) '() (list (child (remove-named-elem (child->element coe) nm))))) (else (list coe))))) ) ;; end module