Re: compiling XPath to Scheme source code
"Oleg A. Paraschenko" <[email protected]> Mon, 29 Nov 2004 11:55:20 +0300
| Newsgroups | gmane.lisp.scheme.ssax-sxml |
|---|---|
| Organization | xmlhack.ru |
| Message-ID | <[email protected]> |
Hello, On Fri, 19 Nov 2004 08:34:29 +0300 "Oleg A. Paraschenko" <[email protected]> wrote: ... > > Your ideas are welcome. > Understanding that dealing with open code is quite error-prone and difficult, I'm still experimenting with this approach. Here are some more ideas why this approach is useful. * Scheme code is more flexible than AST, * it is well known and documented, * it provides a sort of AOP. There is an advantage of Scheme source code over AST. AST is fixed and so it can't provide additional functionality. Scheme source code is flexible, so we can do tricks. For example, consider XPath "elem[position()<3]". Its representation as AST: (relative-location-path (step (axis-specifier (child)) (node-test (local-name "elem")) (predicate (< (function-call (function-name "position")) (number 3))))) Its representation as Scheme code: (node-join (node-reduce (sxml:child (ntype?? 'elem)) (scxpath-filter (scxml:relational-cmp < (scxml:core-position) (xpath-value 3))))) In case of the Scheme code it's possible to change "scxpath-filter" to some "take-until". In case of the AST we can't perform such change without extending AST. Another issue is documentation. Scheme code is documented (R5RS) much better than AST. (Is AST documented at all?) -- Use of "sxlet" allows to make local mofidications to XPath execution. Current SCXPath is used as follows: (define tree '(*top* (AAA (BBB) (CCC) (BBB) (BBB) (DDD (BBB)) (CCC)))) (define xpath-str "/AAA/CCC") (define sc1 (xpath->scheme xpath-str)) (define sc2 (sxlet ((*root* tree)) sc1)) (define x (eval sc2 (interaction-environment))) (pp (x tree)) First, I create a Scheme code from an XPath string, then I bind variables using the "sxlet" macro. Having done that, I use "eval" to make a lambda and finally I apply the lambda to the tree. Result is as expected: ((CCC) (CCC)) But macro "sxlet" not only defines variables, it also can redefine procedures. Example: (define sc2-b (sxlet ( (*root* tree) (sxml:child (lambda (test-pred?) (lambda (nodeset) ((node-trace "child axis (out)") ((sxml:child test-pred?) ((node-trace "child axis (in)") nodeset))))))) sc1)) (define x-b (eval sc2-b (interaction-environment))) (pp (x-b tree)) Now the transcript looks so: -->child axis (in) :(*top* (AAA (BBB) (CCC) (BBB) (BBB) (DDD (BBB)) (CCC))) -->child axis (out) :((AAA (BBB) (CCC) (BBB) (BBB) (DDD (BBB)) (CCC))) -->child axis (in) :(AAA (BBB) (CCC) (BBB) (BBB) (DDD (BBB)) (CCC)) -->child axis (out) :((CCC) (CCC)) ((CCC) (CCC)) So I've affected XPath evaluation without changing Scheme code. It is not a superior archievement, but it may be quite useful. -- Oleg Paraschenko ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://productguide.itmanagersjournal.com/