|
From: Ken A. <kan...@bb...> - 2004-10-01 22:06:25
|
There are at least 3 ways to generate XML in JScheme.
Way 1 - use quasistring:
I'm going to use an example of generating OWL.
(define (datatypeProperty name doc class type)
{<owl:DatatypeProperty <rdf:ID>="[name]">
[doc]
<rdfs:domain rdf:resource="[class]">
<rdfs:range rdf:resource="[(: xsd type)]">
</owl:DatatypeProperty>})
The advantage is you can start with an example of the xml you want and
replace with varible parts with [...].
The disadvantage is your still writing XML mixed with Scheme.
Way 2 - Use a macro like the <> macro in elf/html-gen.scm (which works for html, not xml):
(<> (owl:DatatypeProperty (rdf:ID ,name))
(<> (rdfs:domain (rdf:resource ,class))
(<> (rdfs:range rdf:resource ,(: xsd type)))))
The advantage is you just use s-expressions, using ,name to extract
the value of the name variable from Scheme. The disadvantage is you
have to type <> all the time and understand what "," does.
Way 3 - Geoffrey showed me this approach for his html generation:
(owl:DatatypeProperty (rdf:ID name)
doc
(rdfs:domain (rdf:resource class))
(rdfs:range (rdf:resource (: xsd type))))
Basically you define a procedure with arguments (attributes . contents)
for each tag, and (value) for each attribute.
The advantage is you're completely in Scheme, and evaluating the code
will error if you've made a typo like saying rdf:range.
Static typing in JScheme - who knew!
The disadvantage is you need to write all the functions
Or, just write a macro to generate them:
(define (xtag tag)
(let ((start {<[tag] })
(end {</[tag]>\n}))
(lambda (as . bs)
(if (null? bs) {[start][as]/>\n}
{[start][as]>[bs][end]}))))
(define (xatt name) (lambda (x)
{[name]="[x]"}))
;;; xml attributes - wrap this around more than one attribute.
(define xas string-append)
(define-macro (defxtags . tags)
`(begin
,@(map (lambda (tag) `(define ,tag (xtag ,(.toString tag)))) tags)))
(define-macro (defxatts . tags)
`(begin
,@(map (lambda (tag) `(define ,tag (xatt ,(.toString tag)))) tags)))
(defxtags
owl:Class
owl:DatatypeProperty
owl:ObjectProperty
rdfs:comment
rdfs:domain
rdfs:range
rdfs:subClassOf
)
(defxatts
rdf:datatype
rdf:ID
rdf:resource
)
|