Skip to content

Instantly share code, notes, and snippets.

@ktakashi
Last active December 5, 2017 20:30
Show Gist options
  • Save ktakashi/04d7c00888f43404ef28b9004fcb00a5 to your computer and use it in GitHub Desktop.
Save ktakashi/04d7c00888f43404ef28b9004fcb00a5 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<d:data xmlns:d="defiant-namespace" d:mi="23">
<store d:mi="22">
<book price="12.99" d:price="Number" d:mi="4">
<title d:constr="String" d:mi="1">Sword of Honour</title>
<category d:constr="String" d:mi="2">fiction</category>
<author d:constr="String" d:mi="3">Evelyn Waugh</author>
</book>
<book price="8.99" d:price="Number" d:mi="9">
<title d:constr="String" d:mi="5">Moby Dick</title>
<category d:constr="String" d:mi="6">fiction</category>
<author d:constr="String" d:mi="7">Herman Melville</author>
<isbn d:constr="String" d:mi="8">0-553-21311-3</isbn>
</book>
<book price="8.95" d:price="Number" d:mi="13">
<title d:constr="String" d:mi="10">50</title>
<category d:constr="String" d:mi="11">reference</category>
<author d:constr="String" d:mi="12">Nigel Rees</author>
</book>
<book price="22.99" d:price="Number" d:mi="18">
<title d:constr="String" d:mi="14">The Lord of the Rings</title>
<category d:constr="String" d:mi="15">fiction</category>
<author d:constr="String" d:mi="16">J. R. R. Tolkien</author>
<isbn d:constr="String" d:mi="17">0-395-19395-8</isbn>
</book>
<bicycle price="19.95" d:price="Number" d:mi="21">
<brand d:constr="String" d:mi="19">Cannondale</brand>
<color d:constr="String" d:mi="20">red</color>
</bicycle>
</store>
</d:data>
<?xml version="1.0" encoding="utf-8"?>
<prfx1:data xmlns:prfx1="defiant-namespace" prfx1:mi="23">
<store prfx1:mi="22">
<book prfx1:price="Number" prfx1:mi="4" price="12.99">
<title prfx1:mi="1" prfx1:constr="String">Sword of Honour</title>
<category prfx1:mi="2" prfx1:constr="String">fiction</category>
<author prfx1:mi="3" prfx1:constr="String">Evelyn Waugh</author>
</book>
<book prfx1:price="Number" prfx1:mi="9" price="8.99">
<title prfx1:mi="5" prfx1:constr="String">Moby Dick</title>
<category prfx1:mi="6" prfx1:constr="String">fiction</category>
<author prfx1:mi="7" prfx1:constr="String">Herman Melville</author>
<isbn prfx1:mi="8" prfx1:constr="String">0-553-21311-3</isbn>
</book>
<foo /> <!-- Here is changed -->
<book prfx1:price="Number" prfx1:mi="18" price="22.99">
<title prfx1:mi="14" prfx1:constr="String">The Lord of the Rings</title>
<category prfx1:mi="15" prfx1:constr="String">fiction</category>
<author prfx1:mi="16" prfx1:constr="String">J. R. R. Tolkien</author>
<isbn prfx1:mi="17" prfx1:constr="String">0-395-19395-8</isbn>
</book>
<bicycle prfx1:price="Number" prfx1:mi="21" price="19.95">
<brand prfx1:mi="19" prfx1:constr="String">Cannondale</brand>
<color prfx1:mi="20" prfx1:constr="String">red</color>
</bicycle>
</store>
</prfx1:data>
(import (rnrs)
(text sxml sxpath)
(text sxml ssax)
(text sxml serializer)
(srfi :26))
(define sxml (call-with-input-file "foo.xml" (cut ssax:xml->sxml <> '())))
(define (sxslt sxml)
(lambda (xpath . ns-binding)
(define path (apply sxpath xpath ns-binding))
(define converted (make-eq-hashtable))
;; proc: sxml -> sxml
(lambda (proc)
(for-each (lambda (child)
(hashtable-set! converted child (proc child)))
(path sxml))
(let loop ((sxml sxml))
(cond ((null? sxml) '())
((pair? sxml)
(cond ((hashtable-ref converted sxml #f))
(else (cons (loop (car sxml)) (loop (cdr sxml))))))
(else
(cond ((hashtable-ref converted sxml #f))
(else sxml))))))))
(let ((c ((sxslt sxml) "//*[title='50']")))
(display (srl:sxml->xml (c (lambda (sxml) '(foo))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment