<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RML.io</title>
    <description>The latest articles on DEV Community by RML.io (@rmlio).</description>
    <link>https://dev.to/rmlio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F2149%2F30e5e177-75c8-44a1-979c-d36f9e980e1d.png</url>
      <title>DEV Community: RML.io</title>
      <link>https://dev.to/rmlio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rmlio"/>
    <language>en</language>
    <item>
      <title>Generate RDF from an XML file</title>
      <dc:creator>Pieter Heyvaert</dc:creator>
      <pubDate>Wed, 22 Apr 2020 06:57:26 +0000</pubDate>
      <link>https://dev.to/rmlio/generate-rdf-from-an-xml-file-5b33</link>
      <guid>https://dev.to/rmlio/generate-rdf-from-an-xml-file-5b33</guid>
      <description>&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Before we start the tutorial&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;li&gt;What rules are needed&lt;/li&gt;
&lt;li&gt;How to start a document with RML rules&lt;/li&gt;
&lt;li&gt;What data to use&lt;/li&gt;
&lt;li&gt;How to generate subjects&lt;/li&gt;
&lt;li&gt;How to generate predicates and objects&lt;/li&gt;
&lt;li&gt;Complete Turtle document with RML rules&lt;/li&gt;
&lt;li&gt;Wrapping up&lt;/li&gt;
&lt;li&gt;More information&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id="before-we-start"&gt;1 Before we start the tutorial&lt;/h1&gt;

&lt;h2 id="what-you-learn"&gt;1.1 What you learn&lt;/h2&gt;

&lt;p&gt;At the end of the tutorial you will be able to generate RDF from an XML file using RML rules.&lt;/p&gt;

&lt;h2 id="what-you-need"&gt;What you need&lt;/h2&gt;

&lt;p&gt;We assume that you understand &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/rdf11-concepts/"&gt;RDF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/xml/"&gt;XML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;vocabularies and ontologies, such as classes, properties, and datatypes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="how-to-use"&gt;How you use the tutorial&lt;/h2&gt;

&lt;p&gt;There are two ways to complete this tutorial: you read the explanations and either&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the examples.&lt;/li&gt;
&lt;li&gt;Try out the examples yourself by writing and executing RML rules on your computer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the second option you need a tool that executes RML rules. Suggestions are the &lt;a href="https://github.com/RMLio/rmlmapper-java"&gt;RMLMapper&lt;/a&gt; and the &lt;a href="https://github.com/RMLio/RMLStreamer"&gt;RMLStreamer&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id="example"&gt;2 Example&lt;/h1&gt;

&lt;p&gt;Consider the following XML file called "&lt;a href="https://rml.io/docs/rml/tutorials/xml/characters.xml"&gt;characters.xml&lt;/a&gt;":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;characters&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;character&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;firstname&amp;gt;&lt;/span&gt;Ash&lt;span class="nt"&gt;&amp;lt;/firstname&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;lastname&amp;gt;&lt;/span&gt;Ketchum&lt;span class="nt"&gt;&amp;lt;/lastname&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;hair&amp;gt;&lt;/span&gt;black&lt;span class="nt"&gt;&amp;lt;/hair&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/character&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;character&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;firstname&amp;gt;&lt;/span&gt;Misty&lt;span class="nt"&gt;&amp;lt;/firstname&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;hair&amp;gt;&lt;/span&gt;orange&lt;span class="nt"&gt;&amp;lt;/hair&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/character&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/characters&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It contains the information about two different characters. The id, first name, last name, and hair color are included. The latter two are optional. We want to annotate every character and generate the corresponding RDF triples.&lt;/p&gt;

&lt;p&gt;For example, consider the character described by the first XML element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;character&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;firstname&amp;gt;&lt;/span&gt;Ash&lt;span class="nt"&gt;&amp;lt;/firstname&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;lastname&amp;gt;&lt;/span&gt;Ketchum&lt;span class="nt"&gt;&amp;lt;/lastname&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;hair&amp;gt;&lt;/span&gt;black&lt;span class="nt"&gt;&amp;lt;/hair&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/character&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We want to generate the corresponding RDF triples for this element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
@prefix character: &amp;lt;http://example.org/character/&amp;gt; .

character:0 a schema:Person;
  schema:givenName "Ash";
  schema:lastName "Ketchum";
  dbo:hairColor "black".
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the following sections we explain &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;what rules you need to generate these triples, and &lt;/li&gt;
&lt;li&gt;how you write them using RML.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id="what-rules"&gt;3 What rules are needed&lt;/h1&gt;

&lt;p&gt;Two sets of rules are needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rules that describe the XML file&lt;/li&gt;
&lt;li&gt;rules that define how the RDF terms are generated from the XML file, and how these terms are used to generate triples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our example we need rules that define that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The IRI representing a character is generated by concatenating &lt;code&gt;http://example.org/character/&lt;/code&gt; with the character's id.&lt;/li&gt;
&lt;li&gt;This IRI is used as subject of the triples.&lt;/li&gt;
&lt;li&gt;A character is annotated with the class &lt;a href="http://schema.org/Person"&gt;&lt;code&gt;schema:Person&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The first name is annotated with the property &lt;a href="http://schema.org/givenName"&gt;&lt;code&gt;schema:givenName&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The last name is annotated with the property &lt;a href="http://schema.org/lastName"&gt;&lt;code&gt;schema:lastName&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The hair color is annotated with the property &lt;a href="http://schema.org/hairColor"&gt;&lt;code&gt;dbo:hairColor&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="how-to-start"&gt;4 How to start a document with RML rules&lt;/h1&gt;

&lt;p&gt;We write the RML rules in a &lt;a href="https://www.w3.org/TR/turtle/"&gt;Turtle&lt;/a&gt; document. RML rules are RDF themselves.&lt;/p&gt;

&lt;p&gt;We add the following prefixes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Prefix&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rml&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RML ontology&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rr&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The R2RML ontology, which is extended by RML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ql&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The Query Language vocabulary, which is used together with RML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rdf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The RDF Concepts Vocabulary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;empty&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;The prefix used for our RML rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;schema&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The schema.org vocabulary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dbo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The DBpedia ontology&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last two are added because they are used for the classes and properties.&lt;/p&gt;

&lt;p&gt;The prefixes are added in Turtle like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1 id="what-data"&gt;5 What data to use&lt;/h1&gt;

&lt;p&gt;In our example the data of the characters is stored in an XML file. We add the following RML rules that define what XML file is used and how we iterate over the elements in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source "characters.xml";
    rml:referenceFormulation ql:XPath;
    rml:iterator "/characters/character"
  ].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap a rr:TriplesMap;&lt;/code&gt; 
defines the &lt;a href="https://www.w3.org/TR/r2rml/#dfn-triples-map"&gt;Triples Map&lt;/a&gt;  that groups all rules for the characters.&lt;/li&gt;
&lt;li&gt;The blank node of &lt;code&gt;:TriplesMap rml:logicalSource [ ... ]&lt;/code&gt; contains
all rules about the XML file. The class of the blank node is implicitly of the class &lt;a href="https://rml.io/specs/rml/#logical-source"&gt;&lt;code&gt;rml:LogicalSource&lt;/code&gt;&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rml:source "characters.xml"]&lt;/code&gt; says that we access the XML file &lt;code&gt;characters.xml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rml:referenceFormulation ql:XPath]&lt;/code&gt; says that we use XPath the access the data in the XML file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rml:iterator "/characters/character"]&lt;/code&gt; says that we iterate over all elements  that match the XPath expression &lt;code&gt;/characters/character&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="generate-subjects"&gt;6 How to generate subjects&lt;/h1&gt;

&lt;p&gt;We add the following rules that define how the subject IRI of a character is generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{@id}"
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap rr:subjectMap [ ... ]&lt;/code&gt; contains all the rules about the subject of a triple. The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-subject-map"&gt;&lt;code&gt;rr:SubjectMap&lt;/code&gt;&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:template "http://example.org/character/{@id}"]&lt;/code&gt; says that the IRI of the subject is generated by concatenating &lt;code&gt;http://example.org/character/&lt;/code&gt; with the attribute &lt;code&gt;id&lt;/code&gt; of the character element.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="generate-pos"&gt;7 How to generate predicates and objects&lt;/h1&gt;

&lt;h2 id="class"&gt;7.1 How to annotate with a class&lt;/h2&gt;

&lt;p&gt;In our example we need to annotate every character with the class &lt;code&gt;schema:Person&lt;/code&gt;. We add the following RML rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:constant schema:Person
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap rr:predicateObjectMap [ ... ]&lt;/code&gt; contains all the rules about a specific predicate of a triple. The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-predicate-object-map"&gt;&lt;code&gt;rr:PredicateObjectMap&lt;/code&gt;&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:predicate rdf:type]&lt;/code&gt; says that we use the predicate &lt;code&gt;rdf:type&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:objectMap [ ... ]]&lt;/code&gt; contains all the rules about the object of a triple. The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-object-map"&gt;&lt;code&gt;rr:ObjectMap&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:constant schema:Person]&lt;/code&gt; says that the object of the triple is &lt;code&gt;schema:Person&lt;/code&gt; for every character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting all rules we have so far together results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source "characters.xml";
    rml:referenceFormulation ql:XPath;
    rml:iterator "/characters/character"
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{@id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
    rr:constant schema:Person
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/xml/rules-1.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the following triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person .
&amp;lt;http://example.org/character/1&amp;gt; a schema:Person .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two triples are generated: one for each character. There is a unique subject IRI for each character and each character is annotated with the class &lt;code&gt;schema:Person&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id="property"&gt;7.2 How to annotate with a property&lt;/h2&gt;
 

&lt;p&gt;In our example we need to annotate the values in the tags &lt;code&gt;firstname&lt;/code&gt; &lt;br&gt;
with the property &lt;code&gt;schema:givenName&lt;/code&gt;. We add the following rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The rules are different from when annotating with a class: &lt;code&gt;rml:reference&lt;/code&gt; is used instead of &lt;code&gt;rr:constant&lt;/code&gt; because the object is not the same for every character. More specific, &lt;code&gt;[rml:reference "firstname"]&lt;/code&gt; says that the data in the tag &lt;code&gt;firstname&lt;/code&gt; is used for the object.&lt;/p&gt;

&lt;p&gt;Putting all rules we have so far together results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source "characters.xml";
    rml:referenceFormulation ql:XPath;
    rml:iterator "/characters/character"
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{@id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/xml/rules-2.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the following triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person;
  schema:givenName "Ash" .

&amp;lt;http://example.org/character/1&amp;gt; a schema:Person;
  schema:givenName "Misty" .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two triples are added: one for the first name of each character.&lt;/p&gt;

&lt;p&gt;We add the following rules to annotate the last name and the hair color in the same way as the first name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1 id="complete-turtle"&gt;8 Complete Turtle document with RML rules&lt;/h1&gt;

&lt;p&gt;The complete Turtle document with RML rules is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source "characters.xml";
    rml:referenceFormulation ql:XPath;
    rml:iterator "/characters/character"
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{@id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/xml/rules-3.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the final triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person;
  dbo:hairColor "black";
  schema:givenName "Ash";
  schema:lastName "Ketchum" .

&amp;lt;http://example.org/character/1&amp;gt; a schema:Person;
  dbo:hairColor "orange";
  schema:givenName "Misty" .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1 id="wrapping-up"&gt;9 Wrapping up&lt;/h1&gt;

&lt;p&gt;Congratulations! You have created your own RML rules that generate RDF from data in an XML file. Nice work! We hope you now feel like you have a decent grasp on how RML rules work.&lt;/p&gt;

&lt;h1 id="more-info"&gt;10 More information&lt;/h1&gt;

&lt;p&gt;You can find more information about RML in its &lt;a href="https://rml.io/specs/rml"&gt;specification&lt;/a&gt;. There is also a human readable text-based representation available for RML rules called &lt;a href="https://w3id.org/yarrrml"&gt;YARRRML&lt;/a&gt;. It is a subset of &lt;a href="https://yaml.org/"&gt;YAML&lt;/a&gt;, a widely used data serialization language designed to be human-friendly.&lt;/p&gt;

&lt;p&gt;If you have any questions or remarks, don't hesitate to contact me via &lt;a href="mailto:pieter.heyvaert@ugent.be"&gt;email&lt;/a&gt; or via &lt;a href="https://twitter.com/HeyPieter"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>xml</category>
      <category>rdf</category>
      <category>rml</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Generate RDF from TSV file</title>
      <dc:creator>Pieter Heyvaert</dc:creator>
      <pubDate>Tue, 07 Apr 2020 13:55:54 +0000</pubDate>
      <link>https://dev.to/rmlio/tutorial-generate-rdf-from-tsv-file-18mf</link>
      <guid>https://dev.to/rmlio/tutorial-generate-rdf-from-tsv-file-18mf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Before we start the tutorial&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;li&gt;What rules are needed&lt;/li&gt;
&lt;li&gt;How to start a document with RML rules&lt;/li&gt;
&lt;li&gt;What data to use&lt;/li&gt;
&lt;li&gt;How to generate subjects&lt;/li&gt;
&lt;li&gt;How to generate predicates and objects&lt;/li&gt;
&lt;li&gt;Complete Turtle document with RML rules&lt;/li&gt;
&lt;li&gt;Wrapping up&lt;/li&gt;
&lt;li&gt;More information&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  1 Before we start the tutorial &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1.1 What you learn
&lt;/h2&gt;

&lt;p&gt;At the end of the tutorial you will be able to generate RDF from a TSV file using RML rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you need
&lt;/h2&gt;

&lt;p&gt;We assume that you understand &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/rdf11-concepts/"&gt;RDF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.iana.org/assignments/media-types/text/tab-separated-values"&gt;TSV&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;vocabularies and ontologies, such as classes, properties, and datatypes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How you use the tutorial
&lt;/h2&gt;

&lt;p&gt;There are two ways to complete this tutorial: you read the explanations and either&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the examples.&lt;/li&gt;
&lt;li&gt;Try out the examples yourself by writing and executing RML rules on your computer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the second option you need a tool that executes RML rules. &lt;br&gt;
Suggestions are the &lt;a href="https://github.com/RMLio/rmlmapper-java"&gt;RMLMapper&lt;/a&gt; and the &lt;a href="https://github.com/RMLio/RMLStreamer"&gt;RMLStreamer&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  2 Example &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Consider the following TSV file called "&lt;a href="https://rml.io/docs/rml/tutorials/tsv/characters.tsv"&gt;characters.tsv&lt;/a&gt;":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id firstname lastname hair
0 Ash Ketchum black
1 Misty  orange
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It contains the information about two different characters. The id, first name, last name, and hair color are included. The latter two are optional. We want to annotate every character and generate the corresponding RDF triples.&lt;/p&gt;

&lt;p&gt;For example, consider the character described by the first row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 Ash Ketchum black
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We want to generate the corresponding RDF triples for this row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
@prefix character: &amp;lt;http://example.org/character/&amp;gt; .

character:0 a schema:Person;
  schema:givenName "Ash";
  schema:lastName "Ketchum";
  dbo:hairColor "black".
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the following sections we explain &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;what rules you need to generate these triples, and &lt;/li&gt;
&lt;li&gt;how you write them using RML.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  3 What rules are needed &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Two sets of rules are needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rules that describe the TSV file&lt;/li&gt;
&lt;li&gt;rules that define how the RDF terms are generated from the TSV file, and how these terms are used to generate triples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our example we need rules that define that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The IRI representing a character is generated by concatenating &lt;code&gt;http://example.org/character/&lt;/code&gt; 
with the character's id.&lt;/li&gt;
&lt;li&gt;This IRI is used as subject of the triples.&lt;/li&gt;
&lt;li&gt;A character is annotated with the class &lt;a href="http://schema.org/Person"&gt;&lt;code&gt;schema:Person&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The first name is annotated with the property &lt;a href="http://schema.org/givenName"&gt;&lt;code&gt;schema:givenName&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The last name is annotated with the property &lt;a href="http://schema.org/lastName"&gt;&lt;code&gt;schema:lastName&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The hair color is annotated with the property &lt;a href="http://schema.org/hairColor"&gt;&lt;code&gt;dbo:hairColor&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  4 How to start a document with RML rules &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;We write the RML rules in a &lt;a href="https://www.w3.org/TR/turtle/"&gt;Turtle&lt;/a&gt; document. RML rules are RDF themselves.&lt;/p&gt;

&lt;p&gt;We add the following prefixes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Prefix&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rml&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RML ontology&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rr&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The R2RML ontology, which is extended by RML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ql&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The Query Language vocabulary, which is used together with RML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;csvw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The CSVW Vocabulary, which is used to describe the TSV file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rdf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The RDF Concepts Vocabulary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;empty&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;The prefix used for our RML rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;schema&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The schema.org vocabulary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dbo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The DBpedia ontology&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{.table .table-responsive}&lt;/p&gt;

&lt;p&gt;The last two are added because they are used for the classes and properties.&lt;/p&gt;

&lt;p&gt;The prefixes are added in Turtle like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix csvw: &amp;lt;http://www.w3.org/ns/csvw#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  5 What data to use &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;In our example the data of the characters is stored in a TSV file. We add the following RML rules that define what TSV file is used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap a rr:TriplesMap;&lt;/code&gt; 
defines the &lt;a href="https://www.w3.org/TR/r2rml/#dfn-triples-map"&gt;Triples Map&lt;/a&gt; that groups all rules for the characters.&lt;/li&gt;
&lt;li&gt;The blank node of &lt;code&gt;:TriplesMap rml:logicalSource [ ... ]&lt;/code&gt; contains all rules about the TSV file and how to access the data in the file. The class of the blank node is implicitly of the class &lt;a href="https://rml.io/specs/rml/#logical-source"&gt;&lt;code&gt;rml:LogicalSource&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The blank node in &lt;code&gt;[rml:source [ ... ]&lt;/code&gt; contains all rules about the TSV file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[csvw:url "characters.tsv"]&lt;/code&gt; says that we access the TSV file &lt;code&gt;characters.tsv&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[csvw:dialect [csvw:delimiter "\t"]]&lt;/code&gt; says that the delimiter used in this file is a tab (&lt;code&gt;\t&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rml:referenceFormulation ql:CSV]&lt;/code&gt; says that we use the column names to access the data in the TSV file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that we access a TSV file as if it is a CSV file because we consider it a CSV file with a different delimiter.&lt;/p&gt;

&lt;h1&gt;
  
  
  6 How to generate subjects &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;We add the following rules that define how the subject IRI of a character is generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap rr:subjectMap [ ... ]&lt;/code&gt; contains all the rules about the subject of a triple. The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-subject-map"&gt;&lt;code&gt;rr:SubjectMap&lt;/code&gt;&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:template "http://example.org/character/{id}"]&lt;/code&gt; says that the IRI of the subject is generated by concatenating &lt;code&gt;http://example.org/character/&lt;/code&gt; with the value in the column &lt;code&gt;id&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  7 How to generate predicates and objects &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  7.1 How to annotate with a class
&lt;/h2&gt;

&lt;p&gt;In our example we need to annotate every character with the class &lt;code&gt;schema:Person&lt;/code&gt;. We add the following RML rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:constant schema:Person
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The different rules work as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:TriplesMap rr:predicateObjectMap [ ... ]&lt;/code&gt; contains all the rules about a specific predicate of a triple.
The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-predicate-object-map"&gt;&lt;code&gt;rr:PredicateObjectMap&lt;/code&gt;&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:predicate rdf:type]&lt;/code&gt; says that we use the predicate &lt;code&gt;rdf:type&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:objectMap [ ... ]]&lt;/code&gt; contains all the rules about the object of a triple.
The class of the blank node is implicitly of the class &lt;a href="https://www.w3.org/TR/r2rml/#dfn-object-map"&gt;&lt;code&gt;rr:ObjectMap&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[rr:constant schema:Person]&lt;/code&gt; says that the object of the triple is &lt;code&gt;schema:Person&lt;/code&gt; for every character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting all rules we have so far together results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix csvw: &amp;lt;http://www.w3.org/ns/csvw#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
    rr:constant schema:Person
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/tsv/rules-1.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the following triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person .
&amp;lt;http://example.org/character/1&amp;gt; a schema:Person .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two triples are generated: one for each character. There is a unique subject IRI for each character and each character is annotated with the class &lt;code&gt;schema:Person&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  7.2 How to annotate with a property
&lt;/h2&gt;

&lt;p&gt;In our example we need to annotate the values in the column &lt;code&gt;firstname&lt;/code&gt; with the property &lt;code&gt;schema:givenName&lt;/code&gt;. We add the following rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The rules are different from when annotating with a class: &lt;code&gt;rml:reference&lt;/code&gt; is used instead of &lt;code&gt;rr:constant&lt;/code&gt; because the object is not the same for every character. More specific, &lt;code&gt;[rml:reference "firstname"]&lt;/code&gt; says that the data in the column &lt;code&gt;firstname&lt;/code&gt; is used for the object.&lt;/p&gt;

&lt;p&gt;Putting all rules we have so far together results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix csvw: &amp;lt;http://www.w3.org/ns/csvw#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/tsv/rules-2.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the following triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person;
  schema:givenName "Ash" .

&amp;lt;http://example.org/character/1&amp;gt; a schema:Person;
  schema:givenName "Misty" .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two triples are added: one for the first name of each character.&lt;/p&gt;

&lt;p&gt;We add the following rules to annotate the last name and the hair color in the same way as the first name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  8 Complete Turtle document with RML rules &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;The complete Turtle document with RML rules is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix rml: &amp;lt;http://semweb.mmlab.be/ns/rml#&amp;gt; .
@prefix rr: &amp;lt;http://www.w3.org/ns/r2rml#&amp;gt; .
@prefix ql: &amp;lt;http://semweb.mmlab.be/ns/ql#&amp;gt; .
@prefix csvw: &amp;lt;http://www.w3.org/ns/csvw#&amp;gt; .
@prefix rdf: &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;gt; .
@prefix : &amp;lt;http://example.org/rules/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .
@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can download the Turtle file &lt;a href="https://rml.io/docs/rml/tutorials/tsv/rules-3.rml.ttl"&gt;here&lt;/a&gt;. If we execute these rules, the final triples are generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@prefix dbo: &amp;lt;http://dbpedia.org/ontology/&amp;gt; .
@prefix schema: &amp;lt;http://schema.org/&amp;gt; .

&amp;lt;http://example.org/character/0&amp;gt; a schema:Person;
  dbo:hairColor "black";
  schema:givenName "Ash";
  schema:lastName "Ketchum" .

&amp;lt;http://example.org/character/1&amp;gt; a schema:Person;
  dbo:hairColor "orange";
  schema:givenName "Misty";
  schema:lastName "" .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  9 Wrapping up &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Congratulations! You have created your own RML rules that generate RDF from data in a TSV file. Nice work! We hope you now feel like you have a decent grasp on how RML rules work.&lt;/p&gt;

&lt;h1&gt;
  
  
  10 More information &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;You can find more information about RML in its &lt;a href="https://dev.to/specs/rml"&gt;specification&lt;/a&gt;. There is also a human readable text-based representation available for RML rules called &lt;a href="https://w3id.org/yarrrml"&gt;YARRRML&lt;/a&gt;. It is a subset of &lt;a href="https://yaml.org/"&gt;YAML&lt;/a&gt;, a widely used data serialization language designed to be human-friendly.&lt;/p&gt;

&lt;p&gt;If you have any questions or remarks, don't hesitate to contact me via &lt;a href="mailto:pieter.heyvaert@ugent.be"&gt;email&lt;/a&gt; or via &lt;a href="https://twitter.com/HeyPieter"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tsv</category>
      <category>rdf</category>
      <category>rml</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
