Last modified on 7 March 2014, at 15:17

XQuery/Graph Visualization

Graphviz developed by AT&T provides a package of code for generating graph images from a text definition.

The input file in 'dot' format can be generated by an XQuery script with text output.

MotivationEdit

You want to create a graph to visualize complex structures such as taxonomies, object hierarchies or organizational hierarchies.

Database visualizationEdit

A graphical representation of the relationships between employee and manager in the empdept example.

This script generates the dot format file, with employees as (implicit ) nodes and arcs from employee to manager to show managed by relationships.

The output is serialised as text. Serialisation strips out all XML tags so XML can be used to structure the output and there is no need to serialise each item. The Graphviz dot format uses { } curly brackets as delimiters so these need to be escaped (doubled) in XQuery.

declare option exist:serialize "method=text ";

<graph>
  digraph {{
 { for $emp in //Emp
  let $mgr := //Emp[EmpNo = $emp/MgrNo]
  where exists($mgr)
  return
    concat( $emp/Ename, " -> ", $mgr/Ename, ";")
 }
  }}
</graph>

Generate dot file

If this is now passed through a Graphviz transformer (here a standalone service), we get a graph of these relationships as gif image:

PNG image SVG image

This would look more like a typical organisational chart if the graph was reversed. Graphviz provides a wide range of controls over the content and appearance of the graph.


declare option exist:serialize "method=text ";

<graph>
  digraph {{
  rankdir=BT;
 { for $emp in //Emp
  let $mgr := //Emp[EmpNo = $emp/MgrNo]
  where exists($mgr)
  return
    concat( $emp/Ename, " -> ", $mgr/Ename, ";")
 }
  }}
</graph>

PNG image


Since Enames are not necessarily unique, it would be better to use the EmpNo as the node identifier and label the node with the name:

declare option exist:serialize "method=text ";

<graph>
   digraph {{
   
  {for $emp in //Emp
  let $mgr := //Emp[EmpNo = $emp/MgrNo]
  return
      <emp>
       {$emp/EmpNo} [label="{$emp/Ename}"];
       {if ( exists($mgr))
       then  
          <arc>
            {$mgr/EmpNo}  -> {$emp/EmpNo} ;
          </arc>
       else ()
       }
     </emp>
  }
   }}
</graph>
 

image

Similarly, the Department/Employee Hierarchy can be graphed:

declare option exist:serialize "method=text ";

<graph>
  digraph {{
  {for $dept in //Dept
  return 
  <dept>
        Company  -> {$dept/DeptNo} ;   
        {$dept/DeptNo} [ label="{$dept/Dname}" ];
        { for $emp in //Emp[DeptNo = $dept/DeptNo]
          return
             <emp>
                {$emp/EmpNo}  [label="{$emp/Ename}" ];
                {$dept/DeptNo}  -> {$emp/EmpNo} ;
              </emp>
        }
    </dept>
   }
  }}
</graph>

image