In these exercises we will use the RDF serialisation format Turtle to write RDF.

1.1 Exercise

1.1.1 Getting started

Open a plain text editor, e.g., textpad, gedit, and start the file with the following prefix declarations (ignore the line numbers):

1:  @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
2:  @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
3:  @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
4:  @prefix ex: <http://www.example.org#> .
5:  @prefix w: <http://sws.ifi.uio.no/ont/world.owl#> .

1.1.2 Triples

Continue by adding triples that capture the statements:

  • Norway is called "Norway", using the predicate rdfs:label,
  • Oslo is called "Oslo",
  • Oslo is the capital of Norway—use the predicate w:isCapitalOfCountry,
  • Stavanger is called "Stavanger", and
  • Stavanger is a city in Norway—use the predicate w:isCityInCountry.

Use the namespace prefixed ex: for the resources Norway, Oslo and Stavanger, e.g., ex:Norway.

1.1.3 Validate

Validate your finished RDF file using the RDF Validator and Converter. Paste the contents of your RDF file in the text area on the website and set the input format drop-down menu to "Notation 3 (or N-Triples/Turtle)" and click "Validate!". Sort out any errors in your RDF "code" that the validator reports.

1.1.4 Visualise

When your RDF validates, the website will, in addition to giving you a thumbs up, return an RDF/XML rendering of your file. Copy this RDF/XML, save it to a file—you might need it later, open the W3C's RDF validator, and paste the RDF/XML into the text area. Under Display Result Options select "Triples and Graph" and click "Parse RDF". See the nice graph that appears!

1.1.5 Solution

The code block below contains a solution to the RDF specification in the exercise—excluding the prefixes which should precede this block.

Each line contains a triple. The first triple is

ex:Norway      rdfs:label            "Norway" .

where ex:Norway is the subject of the triple and rdfs:label and "Norway" are the predicate and object, respectively.

 6:  ex:Norway      rdfs:label               "Norway" .
 8:  ex:Oslo        rdfs:label               "Oslo" ;
 9:                 w:isCapitalOfCountry     ex:Norway .
11:  ex:Stavanger   rdfs:label               "Stavanger" ;
12:                 w:isCityInCountry        ex:Norway .

Semicolon is used as shorthand notation; what follows a semicolon specifies a triple with the same subject as the preceding triple without repeating the subject.

An equivalent representation of the lines 8–9, not using semicolon, would be:

 ex:Oslo        rdfs:label              "Oslo" .
 ex:Oslo        w:isCapitalOfCountry    ex:Norway .

1.2 Exercise

Extend the RDF file, i.e., add more triples, to express that

  • Stavanger is City, and Rogaland is a Region—use the predicate rdf:type and the resources w:City and w:Region,
  • Rogaland is a region in Norway, and
  • Stavanger is a city in Rogaland.

Create predicates similar to the predicates in the previous exercise, e.g., isCapitalOfCountry to capture the two last bullet points.

Make sure your extended RDF file validates.

1.2.1 Solution

13:  ex:Stavanger    rdf:type                w:City ;
14:                  w:isCityInRegion        ex:Rogaland .
16:  ex:Rogaland     rdf:type                w:Region ;
17:                  w:isRegionInCountry     ex:Norway .

1.3 Exercise

Further extend your RDF file to contain that:

Norway is a country with a population of 4800000. The head of state is "King Harald V". Norway has two local names, one in the language "Norwegian bokmål" (language code @nb): "Norge", and one the the language "Norwegian nynorsk" (@nn): "Noreg".

Again, create new predicates for the relations between Norway and the information about Norway. It is natural to use literals for the RDF representation of the statements; try also to specify the datatype or language of the literals where appropriate.

1.3.1 Solution

In Turtle a is an abbreviation for rdf:type, so the first line in the block below is equivalent to

 ex:Norway         rdf:type          w:Country ;

The datatype of the literal "4800000" is naturally an integer, this is specified by adding ^^xsd:int behind the literal. Similarly, "Kong Harald V" is a string. 1 The literals "Norge" and "Noreg" are marked with the language they are written in.

18:  ex:Norway       a                       w:Country ;
19:                  w:hasPopulation         "4800000"^^xsd:int ;
20:                  w:hasHeadOfState        "Kong Harald V"^^xsd:string ;
21:                  w:hasLocalName          "Norge"@nb , 
22:                                          "Noreg"@nn .

Like the semicolon, the colon is shorthand notation; what follows a colon specifies a triple with the same subject and predicate as the preceding triple without repeating the subject and predicate. This means that the last line represents the triple

 ex:Norway           w:hasLocalName          "Noreg"@nn .


1 We could have chosen to represent Kong Harald V as a resource, but we chose not to.

Author: Martin G. Skjæveland <martige@ifi.uio.no>

Date: 2010-05-31 08:24:42 CEST

HTML generated by org-mode 6.34trans in emacs 23