[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a new port of oleg's ssax xml



>>>>> "Paul" == Paul T Graunke <ptg@ccs.neu.edu> writes:

>> 
>> Big question: Can we translate the xexpr forms to the SSAX ones?  I think
>> so.  Are there many other serious compatibility issues?

Paul> In theory the answer is clearly yes.  xexpr->xml and ssax's parser will
Paul> translate one to the other.  In practice, the translation will be much
Paul> lighter weight than that, of course.

I use this.  It only does part of the job, but it's fair enough for
what I do:

(module xml-sxml mzscheme
  (require (lib "list.ss")
	   (lib "etc.ss")
           (lib "xml.ss" "xml"))

  (provide xml->sxml)

  (define sxml-normalized (make-parameter #f))

  (define (assoc-sort to-sort)
    (quicksort to-sort (bcompose string<? (compose symbol->string car))))
  
  (define (bcompose f g)
    (lambda (x y) (f (g x) (g y))))

  ;; attribute->srep : Attribute -> Attribute-srep
  (define (attribute->srep a)
    (list (attribute-name a) (attribute-value a)))
  
  (define (xml->sxml x)
    (let* ([non-dropping-combine
	    (lambda (atts body)
	      (cons (cons '@(assoc-sort (map attribute->srep atts)))
		    body))]
	   [combine (if (sxml-normalized)
			non-dropping-combine
			(lambda (atts body)
			  (if (null? atts)
			      body
			      (non-dropping-combine atts body))))])
      (let loop ([x x])
	(cond
	 [(element? x)
	  (let ([body (map loop (element-content x))]
		[atts (element-attributes x)])
	    (cons (element-name x) (combine atts body)))]
	 [(pcdata? x) (normalize-string (pcdata-string x))]
	 [(entity? x) (list (string->symbol
			     (string-append "_"
					    (symbol->string (entity-text x)))))]
	 [(or (comment? x) (pi? x)) x]
	 [(document? x) (error 'xml->xexpr "Expected content, given ~e~nUse document-element to extract the content." x)]
	 [else (error 'xml->xexpr "Expected content, given ~e" x)]))))
  (define (normalize-string s)
    (if (andmap char-whitespace?
		(string->list s))
	""
	s)))

Of course, most of the code is stolen from the current PLT source.

-- 
Cheers =8-} Mike
Friede, Völkerverständigung und überhaupt blabla