Syntax Comparison

Below is a comparison of several popular RDF syntaxes:

and finally... treetriples

Conclusion

This conclusion is obviously a bit biased, but there is certainly room for more than RDF syntax.

There seem to be three main usage patterns for RDF:

For augmenting HTML documents with small amounts of additional metadata, eRDF and RDFa are good choices.

If compatibility with existing XHTML1.x DTDs is a concern, then eRDF is best - if its subset of RDF is sufficient;

If eRDF’s subset of RDF is insufficient, then RDFa is best;

If neither is suitable, then consider using a custom GRDDL transformation.

If you are only planning on reading the entire file into an RDF model, then Turtle or RDF/XML are good choices.

For consumers not using an off-the-shelf RDF API, then Turtle is easier to write a parser for, and is more intuitive.

RXR, TriX, and N-TRIPLES are easy to parse to triples, but their verbosity and poor human-readability may make other formats a better choice.

treetriples is a good choice, although its special support for XML Literals requires support for exclusive canonicalization, which makes parsing to triples slightly harder than RXR.

For accessing and querying the RDF directly with tree-oriented XML technologies, treetriples is ideal.

RXR and TriX are alternatives, but they are verbose, and have poor human readability.  Also, because the triples structure is flat and ungrouped, they are less efficient than treetriples because the entire document needs to be searched for each triple match, whereas treetriples can more efficiently select several triples with the same subject, because only a single branch of the document needs to be searched.

A quick summary of the strengths and weaknesses of various formats

(Just some subjective ranking)

Format RDF library IO Human IO Hackable IO XML tools
RDF/XML 5 4 0 0
N-Triples 4 1 5 0
TriX / RXR 1 1 4 3
Turtle 3
(read as N3)
5 4 0
RDFa / eRDF 2
(GRDDL)
2 3 1
treetriples 2
(GRDDL)
3 4 5

More details are given below:

RDF/XML

[Link to Specification]

Advantages

Disadvantages

Example

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	 xmlns:ex="http://www.example.com/vocab#"
	 xmlns:html="http://www.w3.org/1999/xhtml">

  <rdf:Description rdf:about="http://www.example.com/thing/1/">
    <ex:plain>Plain Literal</ex:plain>
    <ex:lang xml:lang="en">English Literal</ex:lang>
    <ex:bag>
      <rdf:Bag>
	<rdf:li>1</rdf:li>
	<rdf:li>2</rdf:li>
	<rdf:li>3</rdf:li>
      </rdf:Bag>
    </ex:bag>
    <ex:list rdf:parseType="Collection">
      <rdf:Description rdf:about="http://www.example.com/thing/2/" />
      <rdf:Description rdf:about="http://www.example.com/thing/3/" />
    </ex:list>
    <ex:xml rdf:parseType="Literal">
      Below is:
      <html:div>Some <html:b>mixed</html:b> content.</html:div>
    </ex:xml>
    <ex:uri rdf:resource="http://www.example.com/thing/4/" />
    <ex:bnode rdf:nodeID="b2" />
  </rdf:Description>

  <ex:Type rdf:nodeID="b1">
    <ex:number rdf:datatype="http://www.w3.org/2001/XMLSchema#int">7</ex:number>
  </ex:Type>

</rdf:RDF>

N-Triples

N-Triples isn’t intended to be a general purpose interchange format (it was designed as an alternative syntax for RDF/XML test cases), but it is useful nonetheless, and its extreme simplicity is a useful reference point to some of the other syntaxes listed here.

[Link to Specification

Advantages

Disadvantages

Example

<http://www.example.com/thing/1/> <http://www.example.com/vocab#bnode> _:b1 .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#uri> <http://www.example.com/thing/4/> .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#xml> "\n      Below is:\n      <html:div xmlns:html=\"http://www.w3.org/1999/xhtml\">Some <html:b>mixed</html:b> content.</html:div>\n    "^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#list> _:b2 .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#bag> _:b3 .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#lang> "English Literal"@en .
<http://www.example.com/thing/1/> <http://www.example.com/vocab#plain> "Plain Literal" .
_:b4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:b4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.example.com/thing/3/> .
_:b1 <http://www.example.com/vocab#number> "7"^^<http://www.w3.org/2001/XMLSchema#int> .
_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.example.com/vocab#Type> .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#_3> "3" .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2> "2" .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> "1" .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> .
_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:b4 .
_:b2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.example.com/thing/2/> .

TriX

TriX is an XML syntax, that adopts a flat triples style syntax like N-Triples, but it also allows XSLT to be used to provide syntactic extensions that render down to the un-extended TriX format.

[Link to Specification]

Advantages

Disadvantages

Example

<graphset xmlns="http://jena.sourceforge.net/TriX/" >
  <graph>
    <triple>
      <id>_:b1</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri>
      <uri>http://www.example.com/vocab#Type</uri>
    </triple>
    <triple>
      <id>_:b1</id>
      <uri>http://www.example.com/vocab#number</uri>
      <typedLiteral datatype="http://www.w3.org/2001/XMLSchema#int">7</typedLiteral>
    </triple>
    <triple>
      <id>_:b2</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#rest</uri>
      <id>_:b3</id>
    </triple>
    <triple>
      <id>_:b2</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#first</uri>
      <uri>http://www.example.com/thing/2/</uri>
    </triple>
    <triple>
      <id>_:b4</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag</uri>
    </triple>
    <triple>
      <id>_:b4</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#_1</uri>
      <plainLiteral>1</plainLiteral>
    </triple>
    <triple>
      <id>_:b4</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#_2</uri>
      <plainLiteral>2</plainLiteral>
    </triple>
    <triple>
      <id>_:b4</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#_3</uri>
      <plainLiteral>3</plainLiteral>
    </triple>
    <triple>
      <id>_:b3</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#rest</uri>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#nil</uri>
    </triple>
    <triple>
      <id>_:b3</id>
      <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#first</uri>
      <uri>http://www.example.com/thing/3/</uri>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#bnode</uri>
      <id>_:b1</id>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#list</uri>
      <id>_:b2</id>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#bag</uri>
      <id>_:b4</id>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#lang</uri>
      <plainLiteral xml:lang="en">English Literal</plainLiteral>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#plain</uri>
      <plainLiteral>Plain Literal</plainLiteral>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#xml</uri>
      <typedLiteral datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral">
	Below is:
	&lt;html:div xmlns:html="http://www.w3.org/1999/xhtml"&gt;Some &lt;html:b&gt;mixed&lt;/html:b&gt; content.&lt;/html:div&gt;
      </typedLiteral>
    </triple>
    <triple>
      <uri>http://www.example.com/thing/1/</uri>
      <uri>http://www.example.com/vocab#uri</uri>
      <uri>http://www.example.com/thing/4/</uri>
    </triple>
  </graph>
</graphset>

RXR

RXR is similar to TriX, in that it is a simple, triple-based XML serialization of RDF.  It removes syntactic extensibility and named-graphs, but adds a concise syntax for collections.

[Link to Specification]

Advantages

Disadvantages

Example

<graph xmlns="http://ilrt.org/discovery/2004/03/rxr/">
  <triple>
    <subject blank="_:b1" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" />
    <object uri="http://www.example.com/vocab#Type" />
  </triple>
  <triple>
    <subject blank="_:b1" />
    <predicate uri="http://www.example.com/vocab#number" />
    <object datatype="http://www.w3.org/2001/XMLSchema#int">7</object>
  </triple>
  <triple>
    <subject blank="_:b2" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#rest" />
    <object blank="_:b4" />
  </triple>
  <triple>
    <subject blank="_:b2" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#first" />
    <object uri="http://www.example.com/thing/2/" />
  </triple>
  <triple>
    <subject blank="_:b3" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" />
    <object uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag" />
  </triple>
  <triple>
    <subject blank="_:b3" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#_1" />
    <object>1</object>
  </triple>
  <triple>
    <subject blank="_:b3" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#_2" />
    <object>2</object>
  </triple>
  <triple>
    <subject blank="_:b3" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#_3" />
    <object>3</object>
  </triple>
  <triple>
    <subject blank="_:b4" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#rest" />
    <object uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil" />
  </triple>
  <triple>
    <subject blank="_:b4" />
    <predicate uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#first" />
    <object uri="http://www.example.com/thing/3/" />
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#bnode" />
    <object blank="_:b1" />
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#list" />
    <object blank="_:b2" />
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#bag" />
    <object blank="_:b3" />
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#lang" />
    <object xml:lang="en">English Literal</object>
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#plain" />
    <object>Plain Literal</object>
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#xml" />
    <object datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral">
      Below is:
      &lt;html:div xmlns:html="http://www.w3.org/1999/xhtml"&gt;Some &lt;html:b&gt;mixed&lt;/html:b&gt; content.&lt;/html:div&gt;
    </object>
  </triple>
  <triple>
    <subject uri="http://www.example.com/thing/1/" />
    <predicate uri="http://www.example.com/vocab#uri" />
    <object uri="http://www.example.com/thing/4/" />
  </triple>
</graph>

RPV

RPV looks superficially similar to treetriples because it is an XML syntax with concise element names.

[Link to Specification]

Advantages

Disadvantages

Turtle

Turtle is extremely concise, extremely human-readable, and extremely machine-readable.  The only disadvantage is that it isn’t XML.

[Link to Specification]

Advantages

Disadvantages

Example

@prefix :        <#> .
@prefix ex:      <http://www.example.com/vocab#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://www.example.com/thing/1/>
      ex:bag  [ rdf:type rdf:Bag ;
                rdf:_1  "1" ;
                rdf:_2  "2" ;
                rdf:_3  "3"
              ] ;
      ex:bnode
              [ rdf:type ex:Type ;
                ex:number "7"^^<http://www.w3.org/2001/XMLSchema#int>
              ] ;
      ex:lang "English Literal"@en ;
      ex:list (<http://www.example.com/thing/2/> <http://www.example.com/thing/3/>) ;
      ex:plain "Plain Literal" ;
      ex:uri  <http://www.example.com/thing/4/> ;
      ex:xml  "\n      Below is:\n      <html:div xmlns:html=\"http://www.w3.org/1999/xhtml\">Some <html:b>mixed</html:b> content.</html:div>\n    "^^rdf:XMLLiteral .

RDFa

RDFa is an extension of XHTML that allows RDF triples to be embedded in an HTML document.  The work is currently still in progress.

[Link to Specification]

Advantages

Disadvantages

eRDF

[Link to Specification]

Advantages

Disadvantages

Custom GRDDL + XHTML syntaxes

[Link to Specification]

Advantages

Disadvantages

treetriples

The proposal is like an XML reformulation of Turtle, in that it uses nesting to abbreviate subjects with multiple properties and (subject, property) pairs with multiple values.

Like RPV it uses short tag names to reduce verbosity and improve readability.

Unlike most other RDF syntaxes, treetriples has strict requirements of how to serialized documents.  In order to make documents easier to process,

[Link to Specification]

Advantages

Disadvantages

Example

<!DOCTYPE rdf [
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!ENTITY ex "http://www.example.com/vocab#">
]>
<rdf xmlns="http://djpowell.net/schemas/treetriples/1/" xmlns:html="http://www.w3.org/1999/xhtml">

  <s id="http://www.example.com/thing/1/">
    <p id="&ex;bag">
      <o id="_:b1" />
    </p>
    <p id="&ex;bnode">
      <o id="_:b2" />
    </p>
    <p id="&ex;lang">
      <o xml:lang="en">English Literal</o>
    </p>
    <p id="&ex;list">
      <o id="_:b0" />
    </p>
    <p id="&ex;plain">
      <o>Plain Literal</o>
    </p>
    <p id="&ex;uri">
      <o id="http://www.example.com/thing/4/" />
    </p>
    <p id="&ex;xml">
      <o parse="xml">
      Below is:
      <html:div>Some <html:b>mixed</html:b> content.</html:div>
    </o>
    </p>
  </s>

  <s id="_:b2">
    <p id="&rdf;type">
      <o id="&ex;Type"></o>
    </p>
    <p id="&ex;number">
      <o datatype="http://www.w3.org/2001/XMLSchema#int">7</o>	
    </p>
  </s>

  <d parse="list">
    <o id="http://www.example.com/thing/2/" listId="_:b0" />
    <o id="http://www.example.com/thing/3/" />
  </d>

  <d parse="bag" id="_:b1">
    <o>1</o>
    <o>2</o>
    <o>3</o>
  </d>
</rdf>