RDF syntax examples

RDF can be expressed in a variety of different serialization formats. It can also be used inline with HTML. Following is an example of the most widely used of these formats so that you can compare them at a glance.

RDF/XML

An XML-based syntax for RDF graphs that was the first standard format for serializing RDF.

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:gr="http://purl.org/goodrelations/v1#">
  <gr:Location rdf:about="http://www.acme.com/#store">
    <gr:name>Hepp's Happy Burger Restaurant</gr:name>
    <gr:hasOpeningHoursSpecification>
      <gr:OpeningHoursSpecification>
        <gr:opens>08:00:00</gr:opens>
        <gr:closes>20:00:00</gr:closes>
        <gr:hasOpeningHoursDayOfWeek rdf:resource="http://purl.org/goodrelations/v1#Wednesday"/>
        <gr:hasOpeningHoursDayOfWeek rdf:resource="http://purl.org/goodrelations/v1#Monday"/>
        <gr:hasOpeningHoursDayOfWeek rdf:resource="http://purl.org/goodrelations/v1#Tuesday"/>
        <gr:hasOpeningHoursDayOfWeek rdf:resource="http://purl.org/goodrelations/v1#Thursday"/>
        <gr:hasOpeningHoursDayOfWeek rdf:resource="http://purl.org/goodrelations/v1#Friday"/>
      </gr:OpeningHoursSpecification>
    </gr:hasOpeningHoursSpecification>
  </gr:Location>
</rdf:RDF>

See also: RDF 1.1 XML Syntax

Turtle

A compact, human-friendly format.

@prefix gr: <http://purl.org/goodrelations/v1#> .
 
<http://www.acme.com/#store>
  a gr:Location ;
  gr:name "Hepp's Happy Burger Restaurant" ;
  gr:hasOpeningHoursSpecification [
    a gr:OpeningHoursSpecification ;
    gr:opens "08:00:00" ;
    gr:closes "20:00:00" ;
    gr:hasOpeningHoursDayOfWeek gr:Wednesday, gr:Monday, gr:Tuesday, gr:Thursday, gr:Friday
  ] .

See also: Turtle – Terse RDF Triple Language

N-Triples

A very simple, easy-to-parse, line-based format that is not as compact as Turtle.

<http://www.acme.com/#store> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/goodrelations/v1#Location> .
<http://www.acme.com/#store> <http://purl.org/goodrelations/v1#hasOpeningHoursSpecification> _:b0 .
<http://www.acme.com/#store> <http://purl.org/goodrelations/v1#name> "Hepp's Happy Burger Restaurant" .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/goodrelations/v1#OpeningHoursSpecification> .
_:b0 <http://purl.org/goodrelations/v1#opens> "08:00:00" .
_:b0 <http://purl.org/goodrelations/v1#closes> "20:00:00" .
_:b0 <http://purl.org/goodrelations/v1#hasOpeningHoursDayOfWeek> <http://purl.org/goodrelations/v1#Wednesday> .
_:b0 <http://purl.org/goodrelations/v1#hasOpeningHoursDayOfWeek> <http://purl.org/goodrelations/v1#Thursday> .
_:b0 <http://purl.org/goodrelations/v1#hasOpeningHoursDayOfWeek> <http://purl.org/goodrelations/v1#Friday> .
_:b0 <http://purl.org/goodrelations/v1#hasOpeningHoursDayOfWeek> <http://purl.org/goodrelations/v1#Tuesday> .
_:b0 <http://purl.org/goodrelations/v1#hasOpeningHoursDayOfWeek> <http://purl.org/goodrelations/v1#Monday> .

See also: RDF 1.1 N-Triples

N3 (or Notation3)

A non-standard serialization that is very similar to Turtle, but has some additional features, such as the ability to define inference rules.

@prefix gr: <http://purl.org/goodrelations/v1#> .
 
<http://www.acme.com/#store> a gr:Location;
    gr:hasOpeningHoursSpecification [ a gr:OpeningHoursSpecification;
            gr:opens "08:00:00";
            gr:closes "20:00:00";
            gr:hasOpeningHoursDayOfWeek gr:Friday,
                gr:Monday,
                gr:Thursday,
                gr:Tuesday,
                gr:Wednesday ];
    gr:name "Hepp's Happy Burger Restaurant" .

See also: Notation3 (N3): A readable RDF syntax

JSON-LD

a JSON-based serialization (for Linked Data).

{
  "@context": {
    "gr": "http://purl.org/goodrelations/v1#"
  },
  "@id": "http://www.acme.com/#store",
  "@type": "gr:Location",
  "gr:hasOpeningHoursSpecification": {
    "@type": "gr:OpeningHoursSpecification",
    "gr:closes": "20:00:00",
    "gr:hasOpeningHoursDayOfWeek": [
      {
        "@id": "gr:Thursday"
      },
      {
        "@id": "gr:Wednesday"
      },
      {
        "@id": "gr:Friday"
      },
      {
        "@id": "gr:Monday"
      },
      {
        "@id": "gr:Tuesday"
      }
    ],
    "gr:opens": "08:00:00"
  },
  "gr:name": "Hepp's Happy Burger Restaurant"
}

See also: JSON-LD 1.0

RDFa

Not really an RDF syntax, but rather – a compatible format. “RDFa is an extension to HTML5 that helps you markup things like People, Places, Events, Recipes and Reviews. Search Engines and Web Services use this markup to generate better search listings and give you better visibility on the Web, so that people can find your website more easily.” – rdfa.info

<div xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:gr="http://purl.org/goodrelations/v1#">
   <div about="http://www.acme.com/#store" typeof="gr:Location">
      <div rel="gr:hasOpeningHoursSpecification">
         <div typeof="gr:OpeningHoursSpecification">
            <div property="gr:closes" content="20:00:00"/>
            <div rel="gr:hasOpeningHoursDayOfWeek"
                 resource="http://purl.org/goodrelations/v1#Thursday"/>
            <div rel="gr:hasOpeningHoursDayOfWeek"
                 resource="http://purl.org/goodrelations/v1#Tuesday"/>
            <div rel="gr:hasOpeningHoursDayOfWeek"
                 resource="http://purl.org/goodrelations/v1#Monday"/>
            <div rel="gr:hasOpeningHoursDayOfWeek"
                 resource="http://purl.org/goodrelations/v1#Friday"/>
            <div rel="gr:hasOpeningHoursDayOfWeek"
                 resource="http://purl.org/goodrelations/v1#Wednesday"/>
            <div property="gr:opens" content="08:00:00"/>
         </div>
      </div>
      <div property="gr:name" content="Hepp's Happy Burger Restaurant"/>
   </div>
</div>

See also: RDFa

Microdata

Also not really an RDF syntax, but a compatible format. This mechanism allows machine-readable data to be embedded in HTML documents in an easy-to-write manner, with an unambiguous parsing model.

<div itemscope itemtype="http://purl.org/goodrelations/v1#Location" itemid="http://www.acme.com/#store">
  <span itemprop="name">Hepp's Happy Burger Restaurant</span>
  <div itemprop="hasOpeningHoursSpecification" itemscope
       itemtype="http://purl.org/goodrelations/v1#OpeningHoursSpecification">
Opening hours: Mo-Fri,
     <link itemprop="hasOpeningHoursDayOfWeek"
           href="http://purl.org/goodrelations/v1#Monday" />
     <link itemprop="hasOpeningHoursDayOfWeek"
           href="http://purl.org/goodrelations/v1#Tuesday" />
     <link itemprop="hasOpeningHoursDayOfWeek"
           href="http://purl.org/goodrelations/v1#Wednesday" />
     <link itemprop="hasOpeningHoursDayOfWeek"
           href="http://purl.org/goodrelations/v1#Thursday" />
     <link itemprop="hasOpeningHoursDayOfWeek"
           href="http://purl.org/goodrelations/v1#Friday" />
     <meta itemprop="opens" content="08:00:00">8:00 a.m. -
     <meta itemprop="closes" content="20:00:00">8:00 p.m.
  </div>
</div>

See also: HTML Microdata

RDF Converter Services Online

There’s a few decent RDF converter services that you can use online:

SPARQL examples – filter

Return items with recorded date less than given date

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX web: <http://base22.com/ont/web#>
 
SELECT * WHERE {
    ?subject dcterms:title ?title.
    ?subject dcterms:abstract ?abstract.
    ?subject web:publishDate ?publishDate.
    FILTER (?publishDate < "2016-09-28T19:19:02.982Z"^^xsd:dateTime)
}

 

SPARQL examples – limit

Limit the number of results returned

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bc: <http://www.base22.com/ontologies/2010/5/bc.owl#>

SELECT *
WHERE {
  ?subject bc:hasEmployer <http://www.base22.com/ontologies/2010/5/my-contacts.owl#IBM> .
           ?subject bc:hasBusinessEmail ?email .
} LIMIT 5

The LIMIT keyword is used after the WHERE clause, followed by a numerical value. In this case, of course, only 5 results are returned.

  • LIMIT is a solution modifier that limits the number of rows returned from a query. SPARQL has two other solution modifiers:
    • ORDER BY for sorting query solutions on the value of one or more variables
    • OFFSET, used in conjunction with LIMIT and ORDER BY to take a slice of a sorted solution set (e.g. for paging)

Find the person entries in Tim Berners-Lee’s FOAF file that do not contain a URL for the person’s FOAF file. Try this on ARQ.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?name
FROM <http://www.w3.org/People/Berners-Lee/card>
WHERE {
  ?person a foaf:Person ; foaf:name ?name .
  MINUS { ?person rdfs:seeAlso ?url }
}

 

SPARQL examples – paging

Retrieve the second page of names and emails of people in Tim Berners-Lee’s FOAF file, given that each page has 10 people. Try this on ARQ.

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>

SELECT ?name ?email
FROM <http://www.w3.org/People/Berners-Lee/card>
WHERE {
    {      SELECT DISTINCT ?person ?name WHERE {
        ?person foaf:name ?name
      } ORDER BY ?name LIMIT 10 OFFSET 10    }
    OPTIONAL { ?person foaf:mbox ?email }
}

 

SPARQL examples – ask

Is the Amazon river longer than the Nile river?

You can also try this one against DBPedia…

PREFIX prop: <http://dbpedia.org/property/>
ASK
{
  <http://dbpedia.org/resource/Amazon_River> prop:length ?amazon .
  <http://dbpedia.org/resource/Nile> prop:length ?nile .
  FILTER(?amazon > ?nile) .
}

The answer is: false.

Note: The WHERE keyword is optional–not only in ASK queries but in all SPARQL queries.

Does a triple with the given subject URI exist?

ASK { <http://example.org/carbon/ldp/main/people> ?p ?o> }

Returns true or false.

Does Fred have grandchildren?

PREFIX: <http://bedrock/>  
ASK  
WHERE {  :fred :hasChild :?child . :?child :hasChild :?grandchild. }

Where :fred has any child, see if that child has any child identified by the placeholder, :?child, then use the value of that placeholder to ask whether or not that child itself also has a child, which will be the :?grandchild.

Federate data from 2 endpoints

Find the birth dates of all of the actors in Star Trek: The Motion Picture.
Try this on ARQ.

PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?actor_name ?birth_date
FROM <http://www.w3.org/People/Berners-Lee/card> # placeholder graph
WHERE {
  SERVICE <http://data.linkedmdb.org/sparql> {
    <http://data.linkedmdb.org/resource/film/675> movie:actor ?actor .
    ?actor movie:actor_name ?actor_name
  }
  SERVICE <http://dbpedia.org/sparql> {
    ?actor2 a dbpedia:Actor ; foaf:name ?actor_name_en ; dbpedia:birthDate ?birth_date .
    FILTER(STR(?actor_name_en) = ?actor_name)
  }
}

The SERVICE keyword is used to send part of a query against a remote SPARQL endpoint.

SPARQL examples – insert

Insert some statements

PREFIX <http://bedrock/>
INSERT DATA {
    :fred :hasSpouse :wilma .
    :fred :hasChild :pebbles .
    :wilma :hasChild :pebbles .
    :pebbles :hasSpouse :bamm-bamm ;
        :hasChild :roxy, :chip.
}

Insert data into a named graph

INSERT DATA
{
     GRAPH <http://example.org/myGraph> 
     {
          <http://example.org/subject> <http://example.org/predicate> <http://example.org/object>
     }
}

Inserts into a specific Graph. You can insert as many Triples as you want within a single command (only one is used in the example for brevity)

An example SPARQL query filtering for resources within a given date range (between two given dates).

PREFIX c: <https://carbonldp.com/ns/v1/platform#>
 
SELECT ?document ?createdDate WHERE {
    ?document c:created ?createdDate
    FILTER (?createdDate < "2017-04-18T22:29:33.667Z"^^xsd:dateTime && ?createdDate > "2017-04-18T21:37:37.708Z"^^xsd:dateTime)
} LIMIT 100