文章目录
1. RDF
1.1 简介
RDF(Resource Description Framework)语义网的标准数据模型,是一个用于描述 Web 上的资源的框架。RDF本身用XML文件的形式表示。
RDF规定了主-谓-宾这种描述形式(in the form of subject-predicate-object triples)来表示资源之间的关系,RDF语句的集合代表了一个标记的、有方向的图。
RDF被用来作为语义网中其他知识表示和本体语言的基础。
1.2 格式
-
RDF/XML:最古老的语法,几乎被所有的工具所支持,但在任何方面都不人性化
-
RDF/N3 family:紧凑的、人性化的、非XML的语法:N3、NTriples、Turtle
-
Other XML and non-XML syntaxes:TriX, JSON-LD, etc
1.3 RDF的要求
-
识别对象和词汇术语的手段(URIs);
-
区分不同词汇的方法(namespaces and qualified names);
-
RDF语法使用命名空间将URIs缩写为限定名称(QNames)
-
一个 QName 由一个命名空间前缀、一个冒号和一个本地名称组成
例如:rdf:type, dc:creator, foaf:Person -
命名空间前缀对应于URI前缀
例如:给定URI http://www.w3.org/1999/02/22-rdf-syntax-ns# 的命名空间前缀为rdf,则QName rdf:type 的QName将扩展为 http://www.w3.org/1999/02/22-rdf-syntax-ns#type 。
-
-
一种序列化三要素的方法(a data format)。
2. Turtle
Turtle(Terse RDF Triple Language)由于其简单易读,易于编写的特点,成为常见的序列化RDF数据模型的标准。
-
资源URIs写在尖括号里:http://example.org
-
字面量写在双引号中:"like this”
-
三元组以句号结尾:.
-
空白字符(whitespace)不重要
2.1 语法
2.1.1 简单三元组
字面量作为宾语:
<http://www.sciam.com> <http://purl.org/dc/elements/1.1/title> "Scientific American" .
对于较长的字面量,可以使用三双引号。
<http://www.sciam.com> <http://purl.org/dc/elements/1.1/title> """This is a multi-line
literal""" .
2.1.2 Turtle中的命名空间
使用@prefix定义,使用时不需要加尖括号。
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://www.sciam.com> dc:creator <mailto:john @example.org> .
2.1.3 Base URI
@base 引入了一个基础URI,所有URI片段的标识符都是相对于这个URI扩展的(类似于RDF/XML中xml:base的使用)。
@base <http://example.org/data> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#john> foaf:name “John Smith” .
相当于
<http://example.org/data#john> <http://xmlns.com/foaf/0.1/name> “John Smith” .
2.1.4 谓语列表
对于一个主语被多个谓语所引用的情况,它们拥有重复的主语。使用分号 ; 隔开。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
<http://example.org> dc:title "Example Inc. Homepage" ;
dc:creator <mailto:john @example.org> .
2.1.5 宾语列表
与谓语一样,宾语经常与相同的主语和谓语重复。使用逗号 , 隔开。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
<http://example.org> dc:creator <mailto:john @example.org> ,
<mailto:sally@example.org> .
2.1.6 空白节点(bNodes)
有时我们有一些资源,我们不希望用URI来识别它们。这些是空白节点或匿名资源。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<http://www.example.org/> dc:creator [ foaf:name “John Smith” ] .
但是[]语法不足以明确地表示所有包含空白节点的图。例如下面的例子中难以说明这两个空白节点是相同的。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<http://example.org/> dc:creator [ foaf:name “John Smith” ] .
<http://test.example.org/> dc:creator [ foaf:name “John Smith” ] .
空白节点导致的歧义可以通过使用node IDs来解决。Node IDs是RDF图的一个特定序列化的本地标识符。
-
Node IDs看起来像带有_(下划线)命名空间前缀的qNames。
-
例如:. _:a123、_:foo、 _:bar
-
Node IDs不能从定义图的范围之外被引用
当一个RDF文件被解析和序列化时,Node IDs不能保证保持不变:标识符字符串可能会改变,但图的结构将保持不变。使用Node IDs,上述例子可以改变为:
@prefix dc: < http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<http://example.org/> dc:creator _:foo .
<http://test.example.org/> dc:creator _:foo .
_:foo foaf:name “John Smith” .
2.1.7 数据类型
许多应用程序需要能够区分不同类型的字面符号,例如:整数、日期与小数。RDF使用XML Schema数据类型。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://example.org/> dc:date “2020-01-27”^^xsd:date .
RDF支持字面量的语言注释。使用由ISO369-1双字母代码识别的语言:en、zh、fr、de、es。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
<http://example.org/foreword> dc:title “Foreword”@en .
<http://example.org/foreword> dc:title “Avant-propos”@fr .
2.1.8 Class membership
一个对象在一个类中的成员资格是用rdf:type属性表示的。Turtle让我们用’a’来缩写rdf:type。
@prefix ex: <http://example.org/ontology/> .
<http://example.org/> a ex:Website .
2.1.9 空URI
空URI使用<>表示。关于null URIref的断言是关于RDF图本身的。
@prefix dc: < http://purl.org/dc/elements/1.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<> dc:date “2020-01-26”^^xsd:date ;
dc:creator <mailto:nmg@ecs.soton.ac.uk> .
2.1.10 集合
集合(rdf:List)是RDF中表达有序组的一种方式。
-
递归定义:一个集合由集合中的第一个项目和另一个集合组成,后者是剩余的项目。
-
代表一个空集合的资源 :rdf:nil
-
类似于Lisp中的cons/car/cdr列表
rdf:first等同于car:一个集合中的第一个项目
rdf:rest相当于cdr:集合的其余部分
-
不变的:如果不改变集合的形式,就不能改变。
集合在普通RDF中相对不常见,但在OWL的RDF序列化中被广泛使用。
集合的Turtle语法使用圆括号()。
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org> dc:creator ( <mailto:john@example.org>
<mailto:bill@example.org>
<mailto:sally@example.org> ) .
值得注意的是,这两张图说的是根本不同的事情。在上面这张图中,http://example.org/ 只有一个creator,也就是一个容器。但在下面这张图中http://example.org/有三个creator。因此这张图中的三个宾语也没有特定的顺序。
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org> dc:creator <mailto:john@example.org> ,
<mailto:bill@example.org> ,
<mailto:sally@example.org> .