Contents

HO-Utils - XML Extensions

Synopsys

 >(require (lib "xml.scm" "xml"))
 >(require (planet "xml.scm" ("oesterholt" "ho-utils.plt" 1 0)))

 >(define p (open-input-port "some.xml"))
 >(define e (read-xexpr p))
 >(set! e (xexpr-remove-whitespace e))
 >(xpath-xexpr "/sdf-styles/style[1]/name" e)
 ("Style 1")
 >(xpath-xexpr "/*/*/name" e)
 ("Style 1" "Style 2" "Style 3")
 >(xpath-xexpr "/*/*/name[2]" e)
 ("Style 2")
 >(xe-1 e  "/*/*/fg-color[1]/r" (lambda (x) (error "HE!")) string->number)
 232
 >(xe-1 e  "/*/*/qq-color[1]/r" (lambda (x) (error "HE!")) string->number)
 HE!
 >(xe-0-more e "/*/*/fg-color/r" string->number)
 (2 232 4 23)
 >(xe-1-more e "/*/*/tr-color" (lambda (x) (error "ZERO!")) string->number)
 ZERO!
 > (write-xexpr e "new.xml")
 > (define e (read-xexpr "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><a>Hello!<b><c>1</c><d>2</d></b><b>Hi</b></a>"))
 > (xexpr-sax (lambda (element attributes value tree-level intermediate xexprs) 
                   (display (format "~s:~s=~s (~s)~%" tree-level element value intermediate)) 
                   #t)
              e)
 0:a="Hello!" (#t)
 1:b="" (#t)
 2:c="1" (#f)
 2:d="2" (#f)
 1:b="Hi" (#f)

 >(define X (xexpr-elem 'sdf (xexpr-attrs (xexpr-attr 'version 1.0))
                   (xexpr-elems (xexpr-elem 'fg-color "#434343")
                                (xexpr-elem 'bg-color "black"))))

 >(write-xexpr X)

Reading/Writing XML Documents

(read-xexpr port) : xexpr

Reads XML from port and converts it directly to an XExpression, removing all whitespace only values.

(write-xexpr xexpr . port) : void

Writes the xexpr as XML document to the default output port, or port.

Processing X-Expressions

(xexpr-remove-whitespace xexpr) : xexpr

Removes all whitespace only values from xexpr.

(xpath-xexpr xpath e) : xexpr

Evaluates xpath on e and returns the result set for this evaluation, which is a list of XML subexpressions or (end-)values. If the expression doesn't compute, the empty list is returned. If no elements are found, also the empty list is returned.

This function can't handle values for intermediate nodes yet!

precondition: xpath must be a valid XPath and white space must have been removed from xexpr using xexpr-remove-whitespace.

(xe-1 xexpr xpath handler . convertor) : value

Expects 1 result in the result set of (xpath-xexpr xpath xexpr). Returns (convertor applied to) this one result, or the result of handler if 0 or more than 1 results have been returned.

(xe-1-more xexpr xpath handler . convertor) : list of values

Expects 1 or more results in the result set of (xpath-xexpr xpath xexpr). Returns (convertor applied to) this result-set (list of results), or the result of handler if 0 results have been returned.

(xe-0-more xexpr xpath . convertor) : list of values

Expects 0 or more results in the result set of (xpath-xexpr xpath xexpr). Returns (convertor applied to) this result-set (list of results).

(xexpr-get-attr attributes attribute . default) : string

Returns #f (or (car default)), if attribute is not found in attributes.
Returns the associated value with attribute, otherwise.

Can be used in conjunction with the callback that must be provided for xexpr-sax.

(xexpr-sax callback xexpr) : boolean

Traverses the xexpr depth first, calling callback for each element with parameters element, attributes, value, level, intermediate-node and xexprs.

If intermediate-node is #f, this is and end node in the XML tree, otherwise, this node contains other nodes. level gives the current depth in the XML tree, 0 being the top level.

element

is a symbol giving the element name of the current node.

attributes

is a list of attributes (attribute-name:symbol value:string)).

value

is a string containing the value of the current node (works also for intermediate nodes).

xexprs

is '(), if this is an end node. It is a list of all next level nodes, otherwise.

Return value of callback: if #f, xexpr-sax will not go depth first. if #t, xexpr-sax will go depth first. This makes it possible to call the sax parser again with a different callback function on deeper level nodes.

The basic behaviour of xexpr-sax is O(xml-tags) (it traverses all xml-tags of an XML document twice) (first pass assembling the value of a node and the next level nodes of that node; second pass traversing the next level nodes).

Creating X-Expressions

(xexpr-elems first-elements . elements) : xexpr elements

Creates a sequence of xexpr XML elements.

(xexpr-elem element . value|attributes|xexprs{0,3}) : xexpr element

Creates an xexpr element with optional value and/or attributes.

(xexpr-attr attr value) : xexpr attribute

Creates an attribute.

(xexpr-attrs . attributes) : xexpr attributes

Creates the attributes for an xexpression element

Info

Author: Hans Oesterholt-Dijkema, License: LGPL, (c) 2006.