xml->sxml/ file
xml->sxml/ bytes

The "libxml2" Tool

 (require (planet clements/libxml2))

This library is intended to provide bindings to the popular "libxml2" library.

Currently, the only thing it does is use the libxml2 library to parse xml files into sxml, including Relax NG validation. It’s about twice as fast as (planet lizorkin/ssax:2:0/ssax).

It does handle namespaces, in one of the two styles described by SXML. In particular, it prepends the URI itself associated with the namespace to the node’s name. This appears also to be the default behavior of ssax.

This library is completely skeletal. If you want to build a set of bindings to libxml2, use this as a starting point.

(xml->sxml/file path)  sxml?
  path : path-string?

Given a file, produce the sxml that corresponds to it. FWIW, the runtime is completely dominated by the construction of the scheme s-expression; the libxml2 parse function takes about a tenth of the total time.

(xml->sxml/bytes bytes    
  [#:valid validation-context])  sxml?
  bytes : bytes?
  validation-context : validation-context? = #f

Given a byte-string, produce the sxml that corresponds to it. See comments on prior function.

Here’s an example, using the validation context vc defined below:

  (xml->sxml/bytes #"<TARGETDECK><TARGET name=\"Zaronga\"><POSITION /></TARGET></TARGETDECK>"
                   #:valid vc)

(bytes->validation-context bytes 
  [#:valid validation-context]) 
  bytes : bytes?
  validation-context : validation-context? = #f

Given a byte-string representing a Relax NG specification written using the XML syntax, produce a validation context usable with xml->sxml/file and xml->sxml/bytes.

Here’s an example:

  (define vc (bytes->validation-context (string->bytes/utf-8
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<element name=\"TARGETDECK\" xmlns=\"http://relaxng.org/ns/structure/1.0\">\n  <zeroOrMore>\n    <element name=\"TARGET\">\n      <optional>\n        <attribute name=\"name\"/>\n      </optional>\n      <element name=\"POSITION\">\n        <value/>\n      </element>\n    </element>\n  </zeroOrMore>\n</element>")))

(validation-context? v)  boolean?
  v : any/c

Returns true only when v is a value produced by bytes->validation-context.

Let me know of any bugs.