RDF 教程

RDF(Resource Description Framework)教程:从零开始掌握RDF

RDF 是 W3C 制定的语义网(Semantic Web)核心标准,用于描述网页上或者现实世界中的“事物”(资源)及其之间的关系。它是整个语义网技术栈(RDF → RDFS → OWL → SPARQL)的基石。

1. RDF 核心概念:三元组(Triple)

RDF 的所有数据都用“三元组”表示:

<主语> <谓语> <宾语> .

英文叫:Subject → Predicate → Object

举个最经典的例子:

<http://example.org/person/zhangsan> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ontology/Person> .
<http://example.org/person/zhangsan> <http://example.org/ontology/name> "张三" .
<http://example.org/person/zhangsan> <http://example.org/ontology/age> "18"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.org/person/zhangsan> <http://example.org/ontology/knows> <http://example.org/person/lisi> .

翻译成自然语言就是:

  • 张三 是 人
  • 张三 的名字是 “张三”
  • 张三 的年龄是 18(整数)
  • 张三 认识 李四

2. 三元组的三个部分可以是什么?

位置允许出现的类型常用写法举例
主语(Subject)必须是 URI(或者空白节点)http://example.org/person/zhangsan
谓语(Predicate)必须是 URIhttp://example.org/ontology/name
宾语(Object)URI、空白节点、字面量(Literal)“张三” 、 18 、 http://example.org/person/lisi

字面量又分为:

  • 纯字符串:"张三"
  • 带语言标签的字符串:"张三"@zh"Zhang San"@en
  • 带数据类型的字符串:"18"^^xsd:integer"2025-12-05"^^xsd:date

3. 常见的 RDF 序列化格式(语法)

格式扩展名可读性说明
RDF/XML.rdf★☆☆☆☆最老的官方语法,XML 格式,可读性差
Turtle.ttl★★★★★最推荐!人类可读,写起来最舒服
N-Triples.nt★★★★☆每行一条三元组,非常简单
RDFa嵌入HTML★★★☆☆把RDF嵌入到HTML里,SEO常用
JSON-LD.jsonld★★★★☆JSON格式,前端最友好,2025年最流行
N3.n3★★★★☆Turtle的超集,支持公式、集合等

4. 用 Turtle 写一个完整例子(强烈推荐你用这个格式)

@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix :      <http://example.org/> .

:zhangsan rdf:type foaf:Person ;
          foaf:name "张三"@zh , "Zhang San"@en ;
          foaf:age  "18"^^xsd:integer ;
          foaf:knows :lisi , :wangwu ;
          foaf:birthday "2007-01-01"^^xsd:date .

:lisi     rdf:type foaf:Person ;
          foaf:name "李四"@zh .

5. 常用词汇表(你经常会看到的命名空间)

前缀URI说明
rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF 本身
rdfs:http://www.w3.org/2000/01/rdf-schema#RDF Schema层(类、属性定义)
owl:http://www.w3.org/2002/07/owl#本体语言
xsd:http://www.w3.org/2001/XMLSchema#XML数据类型
foaf:http://xmlns.com/foaf/0.1/Friend of a Friend
dc:http://purl.org/dc/elements/1.1/都柏林核心
schema:https://schema.org/Google等人推的结构化数据

6. 实战工具推荐(2025年最新)

工具用途
Protégé免费本体编辑器,支持RDF/OWL
Apache JenaJava框架,读写、推理、SPARQL
RDF4J (原Eclipse RDF4JJava框架,轻量级
rdflib (Python)Python最流行的RDF库
EasyRdf (PHP)PHP操作RDF
JSON-LD Playground在线玩JSON-LD https://json-ld.org/playground/
Turtle Validatorhttps://ttl.sparql.org/

7. 快速上手:10分钟写你的第一份RDF

  1. 新建文件 mydata.ttl
  2. 复制下面内容:
@prefix : <http://example.org/people/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:me rdf:type foaf:Person .
:me foaf:name "你的名字"@zh .
:me foaf:age "30"^^xsd:integer .
:me foaf:knows <https://twitter.com/elonmusk> .
  1. 用浏览器打开 https://ttl.sparql.org/ 粘贴验证一下,没报错就成功了!

8. 下一步学习路线

  1. 掌握 Turtle 语法(最重要!)
  2. 学习 RDFS(定义类、属性、域、范围)
  3. 学习 OWL(更复杂本体建模)
  4. 学习 SPARQL(RDF的“SQL”,查询语言)
  5. 学习 SHACL(RDF数据校验)
  6. 接触知识图谱实际项目(DBpedia、Wikidata、schema.org)

如果你想直接看中文权威教程,推荐:

  • 《语义网技术丛书·RDF/OWL 实战》(机械工业出版社)
  • W3C中文译本:https://www.w3.org/2007/OWL/wiki/Internationalized_Resource_Identifiers_%28IRIs%29
  • 知乎专栏“知识图谱与语义技术”

有任何具体问题(比如想用Python操作RDF、想把MySQL数据转成RDF、想写SPARQL查询等),随时问我,我可以手把手教!

文章已创建 2965

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部