Search

XQuery and Graphviz

I have been using Graphviz for some years now and XQuery is a great language to generate dot files from XML data. eXist lacked the ability to execute a command on the server so I have used a PHP script to convert dot to SVG or GIF. Wolfgang recently added a module to eXist-db 2.0 which supports exec so integration of graphviz can be done entirely in XQuery. Thanks Wolfgang.

Andy Bunce had created a graphviz application in BASEX which had potential for porting to eXist. However much of this code was specific to the BASEX application framework, and initially I wanted a minimal interface to generate SVG. The result was posted to Github and is evolving with help and encouragement from Dan McCreary and Joe Wicentowski.

Via Andy's project I became aware of Martin Loetzch's work to create dotML and XSLT to transform from dotML to dot. This makes it easier to construct dot files from XQuery although I did encounter problems with some files. Actually it's straightforward to create the dot files directly in XQuery with a litlle knowldge of the dot language:

declare function local:manager-graph ($emps) {
<graph> digraph manager {{
 { for $emp in $emps/Emp
   let $mgr := $emps/Emp[EmpNo = $emp/MgrNo]
   let $colour := $local:dept-colours/colour[@dept=$emp/DeptNo]/@name/string()
   return
    <group>
        <node> {$emp/Ename} [ URL="?emp={$emp/EmpNo}" style="filled" fillcolor="{$colour}" ] ;</node>
        { if (exists($mgr)) then <edge> {$mgr/Ename} -> {$emp/Ename} ; </edge> else ()}
    </group>
 }
}}
</graph>
};

Constructing the dot file as XML saves a lot of string handling. The XML tags are arbitrary since the XML is serialised to a string on output. Note the doubled { .

There is a executable but it requires login