XQuery/List OWL Classes

Motivation edit

You want a simple XQuery program that will extract all the OWL classes from an OWL file that is coded using RDF.

Method edit

We will start by just selecting all the classes in the file that have an name. In this example the names are stored in the rdf:ID attribute of the class like the following:

  <owl:Class rdf:ID="Wine">

For our example we will use the wine ontology Used in the W3C OWL Guide:

Our XQuery will specifically get all the RDF tags with the "owl:Class" element in the file.

Here is a simple XQuery that returns all the Classes in the wine ontology. To you this script you can load it into a collection such as /db/apps/owl/views/classes.xq and the RDF data files can be loaded into /db/apps/owl/data

/db/apps/owl/views/classes.xq

xquery version "1.0";

declare namespace xsd="http://www.w3.org/2001/XMLSchema";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";

declare option exist:serialize "method=xhtml media-type=text/html indent=yes";

let $title := 'List of OWL Classes'

let $data-collection := '/db/apps/owl/data'
let $file := request:get-parameter('file', 'wine.rdf')
let $file-path := concat($data-collection, '/', $file)

(: we only want classes that have an ID.  Other classes are not named classes. :)
let $classes := doc($file-path)//owl:Class[@rdf:ID]

(: sort the list :)
let $ordered-classes :=
   for $class in $classes
   order by $class/@rdf:ID
   return $class

return
<html>
    <head>
       <title>{$title}</title>
    </head>
    <body>
      <file>File Path: {$file-path}</file>
      <p>Number of Classes = {count($classes)}</p>
      <ol>
      {for $class in $ordered-classes
         let $class-name := string($class/@rdf:ID)
         return
           <li>{$class-name}</li>
      }
      </ol>
    </body>
</html>

Sample Results edit

The results will be an HTML file with a ordered list:

File Path: /db/org/syntactica/apps/owl/data/wine.rdf

Number of Classes = 74

   1. AlsatianWine
   2. AmericanWine
   3. Anjou
   4. Beaujolais
   5. Bordeaux
   6. Burgundy
   7. CabernetFranc
   8. CabernetSauvignon
   9. CaliforniaWine
  10. Chardonnay
...

Other Tools edit

There are several other tools for working with OWL files that are very useful. One is to list all of the properties in an OWL file or list all the properties of a class. These reports can then be used to load the class or property into an XForms application for editing/versioning/workflow and approval.