0

I got the following XML

<EXAMPLE>   
  <WANNAPRINT>    
   AWESOME  
  </WANNAPRINT>   
  <DONTWANNAPRINT>
   BAD   
  </DONTWANNAPRINT> 
</EXAMPLE>

I work with the given XML using CXML-STP and I only want to print the "wannaprint" section.

I am trying to achieve this using serialize:

(cxml-stp:serialize 
 (let ((last-step 
    (car 
     (cxml-stp:filter-children #'(lambda(node)
                       (and (typep node 'stp:element)
                        (string= (stp:local-name node)
                             "WANNAPRINT")))
                   (cxml-stp:first-child 
                    (cxml:parse 
                     "<EXAMPLE>
  <WANNAPRINT>
   AWESOME
  </WANNAPRINT>
  <DONTWANNAPRINT>
  BAD
  </DONTWANNAPRINT>
 </EXAMPLE>" (stp:make-builder)))))))
   (FORMAT t "last-step:~a~%" last-step)
   last-step)
 (cxml:make-string-sink))

But the result is simply "nil" and I do not understand why, as the printing of the "last-step" variable shows that the correct element has been selected.

How can I serialize/print an element of the cxml-stp:document into a string?

1 Answer 1

1

According to the documentation for cxml:make-string-sink, the string sinks return their strings as a result of a call to sax:end-document, so one thing you could do would be:

(let ((sink (cxml:make-string-sink)))
  (cxml-stp:serialize 
   (let ((last-step 
          (car 
           (cxml-stp:filter-children #'(lambda(node)
                                         (and (typep node 'stp:element)
                                              (string= (stp:local-name node)
                                                       "WANNAPRINT")))
                                     (cxml-stp:first-child 
                                      (cxml:parse 
                                       "<EXAMPLE> <WANNAPRINT> AWESOME </WANNAPRINT> <DONTWANNAPRINT> BAD </DONTWANNAPRINT> </EXAMPLE>"
                                       (stp:make-builder)))))))
     (FORMAT t "last-step:~a~%" last-step)
     last-step)
   sink)
  (sax:end-document sink))

This seems a little icky, but there doesn't seem to be a mechanism exported to get at the ystream inside the sink.

Another way to do it would be to put your element (that is, last-step) in a document. I thought it would be as simple as (stp:make-document last-step) but it complains that the element already has a parent and I don't remember how to get around that right now. In any case, you might not want a document because its serialization includes the prelude (<?xml...?>).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.