URI-Triples: RDF serialization for use in query strings

Hi,

I was pondering RESTful RDF-in-RDF-out web services, i.e. RESTful
services that take RDF as input and return it as output. For POST it's
obvious how to do that, for GET a way to encode graphs in a URI query
string is needed. Of course, you could just %-escape the N-Triples
serialization of a graph, but that's not very readable, and even more
verbose than necessary (and we don't like that in URIs :-)).

So I cooked up a new serialization -- "URI-Triples" -- and wrote a
quick Python parser/serializer.

Here's an example graph in encoded N-Triples:

_:foo%20%3Chttp://www.w3.org/1999/02/22-rdf-syntax-ns%23type%3E%20%3Chttp://example.org/types/Document%3E.%0A_:foo%20%3Chttp://purl.org/dc/elements/1.1/title%3E%20%22War%20and%20Peace%22@en.%0A_:foo%20%3Chttp://purl.org/dc/elements/1.1/title%3E%20%22Krieg%20und%20Frieden%22@de.

Here's the same in URI-Triples:

dc_=_http://purl.org/dc/elements/1.1/_|_rdf_=_http://www.w3.org/1999/02/22-rdf-syntax-ns%23_|_.foo_;_rdf.type_;_http://example.org/types/Document_|_.foo_;_dc.title_;_'War%20and%20Peace'@en_|_.foo_;_dc.title_;_'Krieg%20und%20Frieden'@de

You could also %-encode Turtle, which may sometimes give you even more
brevity, but less readability, because the ; and , delimiters are hard
to see in the URI encoded string; here's an example:

@prefix%20rdf:%20%3Chttp://www.w3.org/1999/02/22-rdf-syntax-ns%23%3E.%20@prefix%20dc:%20%20%3Chttp://purl.org/dc/elements/1.1/%3E.%20_:foo%20rdf:type%20%3Chttp://example.org/types/Document%3E;%20dc:title%20%22War%20and%20Peace%22@en,%20%22Krieg%20und%20Frieden%22@de.

Make up your own mind, but I think the (relative!) brevity and
readability win. When you happen to have one in the location bar of
your browser, you actually stand a chance to figure out how it works
and tinker with it. [Maybe it's just me, but I've always felt web
services ought to be explorable with your browser like that =-)]

You can get the source code from

    http://himalia.it.jyu.fi/~benja/uritriples/

I'm too lazy to do a second write-up of the details, so below, I'll
just copy the code documentation:

"""
A parser and serializer for the URI-Triples serialization of RDF.

Author:   Benja Fallenstein <benja.fallenstein@gmail.com>
Homepage: http://himalia.it.jyu.fi/~benja/uritriples/

WARNING: Currently the parser and serializer perform
only very incomplete checks, i.e. if you pass garbage in,
you may get garbage out instead of an error message.
This should change soon.

URI-Triples is an RDF serialization that you can put into a URI query string.
I just cooked the format up (on 2005-03-13) -- this is gonna be
the reference implementation...

URI-Triples is intended for RESTful RDF-in-RDF-out web services, i.e.
so that you can use GET with web services that take an RDF graph as input.

The syntax is designed to be as readable as I could make it
for something that you can put inside a URI. (If it didn't
have to be readable, you could of course just %-escape N-Triples.)

Here is an example URI-Triples serialization:

dc_=_http://purl.org/dc/elements/1.1/_|_rdf_=_http://www.w3.org/1999/02/22-rdf-syntax-ns%23_|_.foo_;_rdf.type_;_http://example.org/types/Document_|_.foo_;_dc.title_;_'War%20and%20Peace'@en_|_.foo_;_dc.title_;_'Krieg%20und%20Frieden'@de

Here is the same graph in Turtle:

    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
    @prefix dc:  <http://purl.org/dc/elements/1.1/>.

    _:foo rdf:type <http://example.org/types/Document>;
          dc:title "War and Peace"@en, "Krieg und Frieden"@de.

Here is an informal grammar:

query_string := (stmt "_|_")* stmt
stmt         := prefix | triple
prefix       := name "_=_" uri
triple       := (node | bnode) "_;_" node "_;_" (node | bnode | literal)
node         := qname | uri
qname        := name "." name
bnode        := "." name
literal      := "'" text "'" ("" | "@" langtag | "^^" node)

'uri' is a %-escaped version of the UTF-8 serialization of a URIRef 
as defined in the RDF specs.

'text' is a %-escaped version of the UTF-8 serialization of the content
of a literal, as defined in the RDF specs.

I'm not sure about 'name'; it may be just [a-zA-Z0-9]*, or it may be
a %-escaped version of the UTF-8 serialization of an arbitrary string...
do we need the latter for internationalization? But what does it help,
if unquoting (to get the non-ASCII characters) makes the string ambiguous
(as _ could either be from URI-Triples or from the unquoted URI)?

For now, better assume that 'name' is [a-zA-Z0-9]*.
If you have an opinion about this, mail me.
"""

Opinions about URI-Triples, please? :-)

- Benja

Received on Sunday, 13 March 2005 02:42:02 UTC