20040209
james anderson (c)2004,
| [loading] [parsing] [accessor functions] [paths] [combinations] [transformations] |
here is how to use cl-xml to get at components in documents. to begin one needs a document
once the document is parsed, one can use standard common-lisp operators as well as the document component access functions to extract components. for example, in order to print all attributes with the identifier "name" in the element children of the document's root element, one combines standard operators to iterate over the child list, select the element children and ignore the character data, and find the attributes named "name".
in order to print the attribute value, rather than the node itself, the value operator extracts the value from the attribute nodes.
cl-xml defines several kinds of functions which facilitate this kind of access to document components. the simplest are utility functions which combine traversal, type restrictions, and matching. these include the operators
./returns the first element child with a matching name./* collects all element children with a matching name.//* collects all element descendants with a matching name{}inventory,
||::|inventory| (||::section ||::section),and all of the elements in the document are collected by
(||::inventory ||::section ||::item ||::name ||::price ||::description
||::item ||::name ||::price ||::description ||::section ||::item
||::name ||::price ||::description ||::item ||::name ||::price
||::description)("health" "food")("123456789" "445322344" "485672034" "132957764")
for more complex selection and combination operations, one can use binding macros, like destructure-element to designate components by name, position or relation and bind them to variables. this approach can be used to implement simple filters, for example, to transform the original howto document into
a simple account of the items in each section,
== ((||::inventory (||::title . "OmniCorp Store #45x10^3"))
((||::section (||::name . "health")) (||::item "123456789")
(||::item "445322344"))
((||::section (||::name . "food")) (||::item "485672034")
(||::item "132957764"))):eof