cl-sxml
2020-03-25
SXML parsing for Common Lisp
Upstream URL
Author
License
1CL-SXML
CL-SXML implements Oleg Kiselyov's SXML, an S-expression-basedrendering of the XML Infoset. It requires CXML for SAX parsing.CL-SXML is developed on SBCL, but it has been successfully tested on Allegro CL, LispWorks, Clozure & CLISP. It does not currently work with ECL.
Note: CXML:PARSE
behaves differently with different implementations.
With SBCL & Allegro CL, one can just pass it a string to parse; with
LispWorks one must call RUNES:UTF8-STRING-TO-ROD
.
1.1API
1.1.1Class SXML-HANDLER
1.1.1.1Class precedence list
sax:default-handler
, sax:content-handler
, sax:abstract-handler
,sax:sax-parser-mixin
, standard-object
, t
1.1.1.2Description
A CXML SAX handler which produces SXML documents. The constructortakes a single initarg,:PACKAGE
(defaults to *PACKAGE*
), whichspecifies the package to intern symbols into.An interesting omission in the SXML spec is no support for the XML
DOCTYPE declaration. The spec does provide for optional annotations,
though, and so I've chosen to use a *DOCTYPE*
annotation to hold
that information. This may change in the future.
Note that all whitespace is preserved, as required by the XML spec. This is ugly — yet more proof that S-expressions are preferable to XML.
1.1.1.3Example
(cxml:parse "<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Document PUBLIC '-//foo.example//An Example//EN' 'http://foo.example/'>
<?top-level here's a top-level processing instruction?>
<doc xmlns:h='http://www.w3.org/1999/xhtml'>
<h:html>
<h:body>
<h:p class='bar'>Here is some text.</h:p>
</h:body></h:html></doc>"
(make-instance 'cl-sxml:sxml-handler)
:entity-resolver (lambda (p s)
(declare (ignorable p s))
(flexi-streams:make-in-memory-input-stream nil)))
Yields:
(*TOP*
(@
(*DOCTYPE* "Document" "-//foo.example//An Example//EN"
"http://foo.example/"))
(*pi* |top-level| "here's a top-level processing instruction")
(|doc|
(@
(@
(*namespaces*
(|http://www.w3.org/1999/xhtml| "http://www.w3.org/1999/xhtml" |h|))))
"
"
(|http://www.w3.org/1999/xhtml:html| "
"
(|http://www.w3.org/1999/xhtml:body| "
"
(|http://www.w3.org/1999/xhtml:p| (@ (|class| "bar"))
"Here is some text.")
"
"))))