[Cxml-devel] how to create tags with SAX to make an almost identity transformation

Alexandre Rademaker arademaker at gmail.com
Mon Nov 3 12:47:07 UTC 2014


Hi, 

I need to transform all characters | to tags <bar/> in all texts blocks of a big XML file. That is, whenever I found 

<test att="one|two">content | something more | and done</test>

I need to transform to 

<test att="one|two">content <bar/> something more <bar/> and done</test>

Note that | can also occur in attributes values and, in that case, they must be keeped unchanged. Reading the slide http://common-lisp.net/project/cxml/saxoverview/pages/11.html I wrote 

===
(defclass preproc (cxml:sax-proxy) ())

(defmethod sax:characters ((handler preproc) data)
  (call-next-method handler (cl-ppcre:regex-replace "\\|" data "<bar/>")))
===

But of course, it produces a string (escaped) not a tag in the final XML. 

WML> (cxml:parse "<test>content | ola</test>"
                     (make-instance 'preproc
                                    :chained-handler (cxml:make-string-sink)))
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<test>content <bar/> ola</test>"

Any idea or directions? 

Best,

----
Alexandre Rademaker
http://arademaker.github.com






More information about the cxml-devel mailing list