Hi Jon and Alex,

>> Since this is "pure" XML, it would seem natural to find a function like 
>> this <xml> in lib/xml.l. I haven't studied the functions in that file 
>
> Tomas also suggested this. I think it is a good idea. I would like to
> let Tomas make the final decision and/or design.

I take it as punishment for suggesting that;-)

The function you have now looks good except two things:

- no escaping: it is up to the user to escape attribute and body text

- no indenting/formatting: it would be nice to have output formatted
  as the 'xml' function

I was thinking how to best fix these two things and came to the
following which is almost the same code as you have:

(de <xml> Lst
   (let Tag (pop 'Lst)
      (prin "<" Tag)
      (while (and Lst (atom (car Lst)))
         (prin " " (pop 'Lst) "=\"")
         (escXml (eval (pop 'Lst) 1))
         (prin "\"") )
      (ifn Lst
         (prin "/>")
         (prin ">")
         (run Lst 1)
         (prin "</" Tag ">") ) ) )

It is probably best to automatically escape attributes but leave the
user to escape body (as attributes will be usually a value while body
will come via prin or something).

Indenting is impossible to do well I think because the function does
not know what is going to happen in the body (the 'xml' function knows
that) and it is impossible to pass anything to the body as the body is
evaluated in the parent environment.

Not sure whether to remove prinl Jon had there, in general, an XML
schema could specify that white space in an XML element is significant
so the new line would not be desirable.  Probably depends how we plan
to use it.  For now I prefer it without prinl.

Here is a sample output:

: (<xml> hoy id 123
   (<xml> inner1)
   (<xml> text id 123 dx (+ 3 4) dy (* 3 4)
      xx "you & me"
      yy "<![CDATA[\"Me, Myself & <I>\"]]>"
      (prin "No font and color arguments yet") )
   (<xml> inner2 fun "abc"
      (prin "Hi 1 asfdlkasjhfdshad")
      (<xml> line x 0 y 0 dx 100 dy 100
         (prin "thick"))
      (prin "Hi 2") )
   (<xml> inner3 fun "abc") )
<hoy id="123"><inner1/><text id="123" dx="7" dy="12" xx="you &#38; me" 
yy="&#60;![CDATA[&#34;Me, Myself &#38; &#60;I>&#34;]]>">No font and color 
arguments yet</text><inner2 fun="abc">Hi 1 asfdlkasjhfdshad<line x="0" y="0" 
dx="100" dy="100">thick</line>Hi 2</inner2><inner3 fun="abc"/></hoy>-> ">"

The CDATA in attribute does now work well but that is not an issue I
think.  It could be implemented using some kind of convention for
attributes, e.g. (T . "hi") => "<![CDATA[hi]]>" but that is overkill.

To get nicely formated XML, I would use xmllint:

: (let F (tmp "out.xml")
   (out F
      (<xml> hoy id 123
         (<xml> inner1)
         (<xml> text id 123 dx (+ 3 4) dy (* 3 4)
            xx "you & me"
            yy "<![CDATA[\"Me, Myself & <I>\"]]>"
            (prin "No font and color arguments yet") )
         (<xml> inner2 fun "abc"
            (prin "Hi 1 asfdlkasjhfdshad")
            (<xml> line x 0 y 0 dx 100 dy 100
               (prin "thick"))
            (prin "Hi 2") )
         (<xml> inner3 fun "abc")) )
   (call "xmllint" "--format" F) )
<?xml version="1.0"?>
<hoy id="123">
  <inner1/>
  <text id="123" dx="7" dy="12" xx="you &amp; me" yy="&lt;![CDATA[&quot;Me, 
Myself &amp; &lt;I&gt;&quot;]]&gt;">No font and color arguments yet</text>
  <inner2 fun="abc">Hi 1 asfdlkasjhfdshad<line x="0" y="0" dx="100" 
dy="100">thick</line>Hi 2</inner2>
  <inner3 fun="abc"/>
</hoy>
-> T

Or, use the superior 'xml' function to achieve nicely indented XML.

Also, I don't really like the output of 'escXml', it would be more
human friendly to output &amp; etc instead of numbers:

(de escXml (X)
   (for C (chop X)
      (case C
         ("\"" (prin "&quot;"))
         ("&" (prin "&amp;"))
         ("<" (prin "&lt;"))
         (">" (prin "&gt;"))
         (T (prin C)) ) ) )

What do you think?

Cheers,

Tomas
-- 
UNSUBSCRIBE: mailto:[EMAIL PROTECTED]

Reply via email to