10.  RDF/XML Parser and Writer

So far, only S-expression is used to represent RDF ontology. It is convenient for lisp programmers to handle pieces of knowledge in RDF. However, SWCLOS, of course, allows users to read RDF/XML format files and print out SWCLOS contents in RDF/XML format.

10.1  RDF/XML Parser

parse-rdf is a parser for RDF/XML format files. It is useful to check RDF/XML syntax, but it does not interpret anything in semantics. In the following example, you might misunderstand that parse-rdf just prints out an RDF/XML format file, but it is not a reality. parse-rdf produces a list of XML element of lisp structure from RDF/XML file, namely XMLDecl, doctypedecl, Comment, and RDFdecl element as lisp structure. In Common Lisp, the print form of lisp structure is programmable. So, the structure print function of XMLDecl is programmed so as to print out a XMLDecl form in XML, the print function of doctypedecl structure is to “:doctypedecl ...”, a Comment structure to a Comment form, and an RDFdecl structure to an RDFdecl form. In short, parse-rdf reads RDF/XML format file, parses RDF/XML format, makes structures, and returns them in a list. Then, you see RDF/XML-like forms of such structures in the list.

If you want to handle just RDF data without the interpretation of RDFS and OWL from RDF/XML format files, parse-rdf is available to do so. However, in order to interpret contents in RDF/XML files, you may use read-rdf-file with an accepter function, which is usually addRdfXml. See the followings.

gx-user(2): (with-open-file (p "RDFS/JenaEx.rdf") (parse-rdf p))
(<?xml version="1.0" ?> #<doctypedecl ... >
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#"
         xmlns:somewhere="http://somewhere/">
  <rdf:Description rdf:about="http://somewhere/JohnSmith">
    <vCard:FN>John Smith</vCard:FN>
    <vCard:N>
      <rdf:Description>
        <vCard:Family>Smith</vCard:Family>
        <vCard:Given>John</vCard:Given>
      </rdf:Description>
    </vCard:N>
  </rdf:Description>
</rdf:RDF>)
gx-user(3): (read-rdf-file #'addRdfXml "RDFS/JenaEx.rdf")
Warning: Entail by rdf1: vCard:FN rdf:type rdf:Property.
Warning: Entail by rdf1: vCard:N rdf:type rdf:Property.
Warning: Entail by rdf1: vCard:Family rdf:type rdf:Property.
Warning: Entail by rdf1: vCard:Given rdf:type rdf:Property.
:done
gx-user(4): somewhere:JohnSmith
#<|rdfs:Resource| somewhere:JohnSmith>

parse-rdf stream
[Function]
parses RDF/XML from stream and makes the internal representation of RDF. This function returns a list of several XML structures. Note that *base-uri* and *default-namespace* in the environment are updated through content from stream.

read-rdf-file accepter-fun [ file [ code ]]
[Function]
This function reads and parses a file that contains RDF/XML format data, then passes attributes in RDFdecl, parsed XMLDecl, parsed doctypedecl, parsed Comments, and parsed Descriptions to accepter-fun. The code is a character code of file, of which the default is utf-8. If file has its encoding description in XMLDecl, the direction in file supersedes the code.

addRdfXml description
[Function]
This function accepts an instance of lisp structure Description, which may be generated from the RDF parser, and transform it to S-expression form, then interprets the form by addForm. This function returns the result of addForm.

10.2  RDF/XML Writer

To print out RDF data in RDF/XML format, the function write-xml is available. See the following example.

gx-user(5): (write-xml somewhere:JohnSmith)
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:somewhere="http://somewhere/"
         xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" >
  <rdf:Description rdf:about="http://somewhere/JohnSmith" >
    <vCard:FN>John Smith</vCard:FN>
    <vCard:N>
      <rdf:Description>
        <vCard:Family>Smith</vCard:Family>
        <vCard:Given>John</vCard:Given>
      </rdf:Description>
    </vCard:N>
  </rdf:Description>
</rdf:RDF>

write-xml x [ stream ]
[Function]
This function accepts a resource object or a list of resource objects, and print out them in RDF/XML format. Note that this function returns no value. If stream is not supplied, *standard-output* is used as default.


Author:
Seiji Koide. Copyright (c) 2005, 2006 GALAXY EXPRESS CORPORATION. Feb. 2006
Copyright (c) 2007, 2010 Seiji Koide. Oct.2010