treeql

https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/treeql/treeql.md


TreeQL is a cost-like tree editing and query language.

The fundamental idea is to require of a tree-like object a simple API [L1 ] and to use that to generate and filter a list of tree nodes by using methods which map or apply methods and predicates to the nodes in the node set.

The language form is very tcl-ish and quite free-form, which was entirely inspired by Cost.

I should point out, in passing, that this approach is quite non-standard, and there are other more standard approaches to the problem of searching and editing trees, such as the excellent tdom.

20041001 treeql is now part of tcllib. AKU ported it to tcl8.4, and helped a lot with QA.


TreeQL adaptor and Web Scraping with htmlparse have some examples using treeql.

I've put TreeQL docs here [L2 ] and the current version here: [L3 ] - CMcC Updated: 20040820

Included with the library are a couple of adaptors, one to give file systems tree-semantics which gives file systems treecl/cost semantics. Beats the hell out of [glob] :)

Another adaptor is doc, which uses parsehtml and the shallow xml parser to generate trees for scanning.

I have to point out, however, this is very much a work in progress.


Some Examples from the included doc.tcl

Create a doc given a URL:

    doc homepage -url http://sharedtech.dyndns.org/

Create a Costq query over the doc tree:

    Costq nh -tree homepage

Print only the PCDATA content of the page:

    puts "TEXT: [nh query tree content]"

Print the href attributes of all <a> tags:

    puts "A: [nh query tree withtag a attval href]"

Another way to do the same:

    puts "A2: [nh query tree hasatt href attval href]"

Print the size of the tree:

    puts "SIZE: [homepage size]"

[old discussion removed]

20041006 - Brian Theado - Nice work on this package. I pulled the depedencies out of the candidate tcllib 1.7 and made it into a sourceable starkit at http://tkoutline.sourceforge.net/treeql.kit .

Here is one unexpected behavior I found:

    source treeql.kit
    package require treeql
    package require struct::tree
    package require tcltest
    namespace import tcltest::*
    set t [struct::tree]; ::treeql q -tree $t
    test get.1 {attributes with special characters} {
        $t insert root end n1 n2 n3
        $t set n1 title hello
        $t set n2 title "hello there"
        $t set n3 title {[hello]}
        q query root children get title
    } [list hello {hello there} {[hello]}]
    $t destroy; q destroy

Output:

 ---- Result was:
 hello {{{hello there}}} {{{[hello]}}}
 ---- Result should have been (exact matching):
 hello {hello there} {[hello]}
 ==== get.1 FAILED

dbohdan 2015-01-11: This neat little language appears to be severely underused by tclers. Consider that it provide access to hierarchical documents in the age of the Web and is already there in Tcllib. The lack of examples on the wiki may be one reason for it, so I've added the one I wrote while figuring it out at Web Scraping with htmlparse (processing trees generated by htmlparse seems like the killer app for TreeQL). The man page could use more examples as well.

See also