On this page:
write-turtle-nsmap
nsmap->turtle-string
write-turtle-graph
graph->turtle-string
write-trig-graph
graph->trig-string
write-trig-dataset
dataset->trig-string

14.3 Turtle/TriG Formatting🔗ℹ

The Turtle/TriG writer provided in the core emits a sub-set of the Turtle syntax, as described below.

turtle-graph    ::= turtle-header turtle-content

turtle-header   ::= base-decl? namespace-decl*

turtle-content  ::= ( triple | statements )*

base-decl       ::= "@base" resource "."

namespace-decl  ::= "@prefix" prefix resource "."

prefix          ::= LOCAL_NAME? ":"

statements      ::= subject predicates "."

predicates      ::= predicate objects ( ";" predicate objects )*

objects         ::= object ( "," object )*

Turtle also allows for the use of prefixed names instead of direct IRIs for resources, the following re-definitions for subject, predicate, and object show this.

subject         ::= resource | prefixed-name | blank-node

predicate       ::= resource | prefixed-name

object          ::= resource | prefixed-name | blank-node | literal

prefixed-name   ::= prefix LOCAL_NAME?

The extension of Turtle into TriG is to add a graph identifier to wrap a set of Turtle statements so that multiple graphs can be serialized into the same resource.

trig-dataset    ::= turtle-header trig-graph*

trig-graph      ::= graph-name? "{"  turtle-content "}"

procedure

(write-turtle-nsmap val [out])  void?

  val : nsmap?
  out : output-port? = (current-output-port)

procedure

(nsmap->turtle-string val)  void?

  val : nsmap?
Write a nsmap to the provided output port, or to a string. This string will use Turtle syntax in the output.

Examples:
> (require rdf/core/io
           rdf/core/nsmap)
> (write-turtle-nsmap (make-common-nsmap))

@prefix owl: <http://www.w3.org/2002/07/owl#> .

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@prefix dcterms: <http://purl.org/dc/terms/> .

@prefix dc: <http://purl.org/dc/elements/1.1/> .

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

> (displayln
    (nsmap->turtle-string (make-common-nsmap)))

@prefix owl: <http://www.w3.org/2002/07/owl#> .

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@prefix dcterms: <http://purl.org/dc/terms/> .

@prefix dc: <http://purl.org/dc/elements/1.1/> .

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

procedure

(write-turtle-graph val [out])  void?

  val : graph?
  out : output-port? = (current-output-port)

procedure

(graph->turtle-string val)  string?

  val : graph?
Write a graph? to the provided output port, or to a string, in Turtle syntax.

procedure

(write-trig-graph val [out])  void?

  val : graph?
  out : output-port? = (current-output-port)

procedure

(graph->trig-string val)  string?

  val : graph?
Write a graph? to the provided output port, or to a string, in TriG syntax.

procedure

(write-trig-dataset val [out])  void?

  val : dataset?
  out : output-port? = (current-output-port)

procedure

(dataset->trig-string val)  string?

  val : dataset?
Write a dataset? to the provided output port, or to a string, in TriG syntax.