[Cxml-devel] how to create tags with SAX to make an almost identity transformation
Russ Tyndall
russ at acceleration.net
Mon Nov 3 15:35:38 UTC 2014
Howdy,
You will need to issue sax:start-element and sax:end-element calls
instead of doing a string replace.Essentially you will replace the
single sax:characters call with a series of characters / elements calls.
EG:
(defclass preproc (cxml:sax-proxy) ())
(defmethod sax:characters ((handler preproc) data)
(let ((chunks (cl-ppcre:split "\\|" data)))
(if (= 1 (length chunks))
(call-next-method)
(loop for c in chunks
for first? = t then nil
do (unless first?
(sax:start-element handler nil nil "bar" nil)
(sax:end-element handler nil nil "bar"))
(sax:characters handler c)))))
(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>"
Hope this helps,
Russ Tyndall
Acceleration.net
On 11/03/2014 07:47 AM, Alexandre Rademaker wrote:
> 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
>
>
>
> _______________________________________________
> Cxml-devel mailing list
> Cxml-devel at common-lisp.net
> http://mailman.common-lisp.net/cgi-bin/mailman/listinfo/cxml-devel
More information about the cxml-devel
mailing list