XQuery/Graphing Triples

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The RDF Validation service can be used to graph RDF, but since this expands prefixed names to full URIs the graphs can look rather un-readable as examples.

This service is for drawing simple triple graphs where each triple is defined in a local XML format in which each triple has attributes subject, property and object. [XQuery function to convert RDF to N3 needed]

Subjects and objects are drawn as nodes, triples as arcs with the property as the label. If the subject or object contains ':' or starts with 'http://', then the node is shown as an ellipse. If it starts with '_' it is a blank node and an unnamed circle is shown; otherwise the node is assumed to be a literal and is drawn in a box.

Endpoint[edit | edit source]


Heading text[edit | edit source]

Parameters[edit | edit source]

  • url : url of triples in the xml format illustrated above
  • dir : LR - left to right (default), TB - top to bottom [ rankdir in Graphviz]
  • title : title for graph - default none

Example[edit | edit source]

From page 11 of the RDF primer

<?xml version="1.0" encoding="UTF-8"?>
    <triple subject="exstaff:85740" property="exterms:address" object="exaddressid:87540"/>
    <triple subject="exaddressid:87540" property="exterms:street" object="1501 Grant Avenue"/>
    <triple subject="exaddressid:87540" property="exterms:city" object="Bedford"/>
    <triple subject="exaddressid:87540" property="exterms:state" object="Massachusetts"/>
    <triple subject="exaddressid:87540" property="exterms:postalcode" object="01730"/>

dot Output

digraph { rankdir='LR' 
"exstaff:85740" [label="exstaff:85740" shape=ellipse];
"exaddressid:87540" [label="exaddressid:87540" shape=ellipse];
"1501 Grant Avenue" [label="1501 Grant Avenue" shape=box];
"Bedford" [label="Bedford" shape=box];
"Massachusetts" [label="Massachusetts" shape=box];
"01730" [label="01730" shape=box];
"exstaff:85740" -> "exaddressid:87540" [label="exterms:address"];
"exaddressid:87540" -> "1501 Grant Avenue" [label="exterms:street"];
"exaddressid:87540" -> "Bedford" [label="exterms:city"];
"exaddressid:87540" -> "Massachusetts" [label="exterms:state"];
"exaddressid:87540" -> "01730" [label="exterms:postalcode"];

GIF Image


Usage[edit | edit source]

Either save the generated gif, or use 5clicks or similar to capture the image on the screen. One way to print large GIF images is to save the image, then insert it into an Excel spreadsheet. Excel will print the image over multiple pages. Reduced in size and with page borders removed, even large graphs can be printed and then taped together.

Source[edit | edit source]

declare option exist:serialize "method=text media-type=text/text";
declare variable $nl := "&#10;";
declare variable $url := request:get-parameter("url",());
declare variable $dir := request:get-parameter("dir","LR");
declare variable $title := request:get-parameter("title","");

let $graph := doc($url)
return   ( 
  "digraph ",$title, " { rankdir='" , $dir,"' ", $nl,
   for $node in distinct-values(($graph//triple/@subject,$graph//triple/@object))
   let $nodetype := 
     if (contains($node,":") or starts-with ($node,"http://")) 
     then concat ('label="',$node,'" shape=ellipse') 
     else if (starts-with($node,"_"))
     then 'shape=circle'
     else concat ('label="',$node,'" shape=box') 
     concat ('"',$node,'" [',$nodetype,'];',$nl)
     for $triple in $graph//triple
      ( concat ('"', $triple/@subject, '" -> "' , $triple/@object ,'" [label="',$triple/@property, '"];'), $nl)
   "} ",$nl

This script would be improved by the use of an intermediate XML structure and a XSLT script to convert to dot.