RDF/XML and N3

A light introduction to their syntax

Created by Nils N. Haukås and licenced under Creative Commons CC BY 3.0.

RDF/XML


  
  

  
    
      
    

  
    
      
    
  
            

Note the use of xml namespaces. Click down arrow to see translation.

N3


@prefix homeMadeOnt: <http://myBlog.com/homemade-ont/>.
@prefix work: <http://someOntologyBase.org/tech-positions#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

#With N3 triples become oneliners of subject predicate object

<http://someSocialNetwork.com/people#mari> homeMadeOnt:hasJob work:sysadmin.

# Comments are made using the '#' sign
            
            

Click up arrow to go back. All the rdf/xml have corresponding N3 translations.

Multiple property elements XML




  
    
      
    
    
      
    
  


            

Multiple property elements N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:knows sns:mina,
                  sns:ole.
            

Empty property elements XML




  
    
      
    
      
  


            

Empty property elements N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:knows sns:mina,
                  sns:ole.
            

The corresponding n3 remains the same.

Property attributes XML




  
    
    Greg Doe 
  
   
     
  


            

Property attributes N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:knows sns:mina;
  foaf:name "Greg Doe".

sns:jane foaf:knows sns:greg;
  foaf:name "Jane Doe".
            

XML literals




  
    
      
        Jane Doe
      
    
  
  

            

XML literals N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix xmlns: <http://www.w3.org/2000/xmlns/>.
@prefix ex: <http://someExampleOntology.org/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg ex:hasPhone "
  <a:rawContactXML xmlns:a=\"http://phoneXMLNamespace.org/a#\">
    <a:contact celNum=\"123 456 78\">Jane Doe</a:contact>
  </a:rawContactXML> 
  "^^rdf:XMLLiteral.

#The above can be written in one line
sns:greg ex:hasPhone "<a:rawContactXML xmlns:a=\"http://phoneXMLNamespace.org/a#\"> <a:contact celNum=\"123 456 78\">Jane Doe</a:contact> </a:rawContactXML> "^^rdf:XMLLiteral.

            

Typed Literals rdf:datatype




  
    Greg Doe
    24
  


            

N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:age 24;
  foaf:name "Greg Doe"^^<http://www.w3.org/2001/XMLSchema#string>.
            

Identifying blank nodes - rdf:nodeID




  
    
      
    
     
  

   
    Jane Doe
    http://jane.blogg.no/
  


            

Identifying blank nodes - rdf:nodeID


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:knows sns:john,
    _:abc.

_:abc foaf:homepage "http://jane.blogg.no/";
  foaf:name "Jane Doe".
            

Omiting blank nodes



  
     
     
      
        http://jane.blogg.no/
      
    
    
     
      John Doe
      http://john.blogg.no/  
    
  

            

Omiting blank nodes - N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg foaf:knows _:bnode136700096,
    _:bnode800626944.
_:bnode136700096 foaf:homepage "http://john.blogg.no/";
  foaf:name "John Doe".
_:bnode800626944 foaf:homepage "http://jane.blogg.no/";
  foaf:name "Jane Doe".
            

Omitting nodes, continued




  
     
      John Doe
      http://john.blogg.no/  
    

    
    

  


            

Omitting nodes: Property attributes on an empty property element

Omitting nodes, continued - N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

#Here Greg knows to people who *might* be the same person.

sns:greg foaf:knows _:bnode1421465472,
    _:bnode134694720.
_:bnode134694720 foaf:homepage " http://john.blogg.no/";
  foaf:name "John Doe".
_:bnode1421465472 foaf:homepage "http://john.blogg.no/";
  foaf:name "John Doe".
            

Typed node elements




  
  
     
    Greg Doe
  

  
  
    Jane Doe      
  


            

Typed node elements - N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

sns:greg a foaf:Person;
  foaf:name "Greg Doe".
sns:jane a foaf:Person;
  foaf:name "Jane Doe".
            

Abbreviating URIs with xml:base




  
  
   
     
    
  


            

These abbreviations are possible for rdf:about, rdf:resource, rdf:ID and rdf:datatype.

Abbreviating URIs with xml:base N3


@prefix sns: <http://someSocialNetwork.com/people#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<http://newsocialnetwork.com/persons/#Margareth> foaf:knows sns:Greg,    
    <http://newsocialnetwork.com/persons/Morten>.
            

Other RDF/XML tricks

  • The <rdf:RDF></rdf:RDF> top node may be omited if the remaining rdf/xml has a single top node like <rdf:Description></rdf:Description>.
  • xml:lang="" may be used on any node element or property element to indicate the language.
  • For more info check http://www.w3.org/TR/rdf-syntax-grammar/.