目录

XML RDF


RDF 文档示例

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:si="https://www.91xjr.com/rdf/">

<rdf:Description rdf:about="https://www.91xjr.com">
  <si:title>91xjr</si:title>
  <si:author>Jan Egil Refsnes</si:author>
</rdf:Description>

</rdf:RDF>

什么是RDF?

  • RDF 代表R资源D描述F框架
  • RDF 是一个用于描述网络资源的框架
  • RDF 旨在被计算机读取和理解
  • RDF 并不是为向人们展示而设计的
  • RDF是用XML编写的
  • RDF 是 W3C 语义 Web 活动的一部分
  • RDF 是 2004 年 2 月 10 日起的 W3C 推荐标准

RDF - 使用示例

  • 描述购物商品的属性,例如价格和可用性
  • 描述网络事件的时间表
  • 描述有关网页的信息(内容、作者、创建和修改日期)
  • 描述网页图片的内容和评级
  • 为搜索引擎描述内容
  • 描述电子图书馆

RDF 设计用于计算机读取

RDF 旨在提供一种描述信息的通用方法,以便计算机应用程序可以读取和理解信息。

RDF 描述并不是为了在 Web 上显示而设计的。



RDF 用 XML 编写

RDF 文档是用 XML 编写的。 RDF 使用的 XML 语言称为 RDF/XML。

通过使用 XML,RDF 信息可以轻松地在使用不同类型操作系统和应用程序语言的不同类型计算机之间交换。


RDF 和"The Semantic Web"

RDF 语言是 W3C 语义 Web 活动的一部分。 W3C 的"Semantic Web Vision" 是一个未来:

  • 网络信息具有确切含义
  • 网络信息可以被计算机理解和处理
  • 计算机可以整合来自网络的信息

RDF 使用 Web 标识符 (URI) 来标识资源。

RDF 用属性和属性值来描述资源。


RDF 资源、属性和属性值

RDF 使用 Web 标识符 (URI) 来标识事物,并使用属性和属性值来描述资源。

资源、属性和属性值的说明:

  • A资源是任何可以有 URI 的内容,例如 "https://www.91xjr.com/rdf"
  • A属性是具有名称的资源,例如 "author" 或 "homepage"
  • A适当的值是属性的值,例如 "Jan Egil Refsnes" 或 "https://www.91xjr.com"(请注意,属性值可以是另一个资源)

以下 RDF 文档可以描述资源"https://www.91xjr.com/rdf":

<?xml version="1.0"?>

<RDF>
  <Description about="https://www.91xjr.com/rdf">
    <author>Jan Egil Refsnes</author>
    <homepage>https://www.91xjr.com</homepage>
  </Description>
</RDF>

上面的例子是经过简化的。命名空间被省略。


RDF 声明

资源、属性和属性值的组合形成一个声明(称为主语、谓语和宾语声明)。

让我们看一些示例语句以更好地理解:

声明:"The author of https://www.91xjr.com/rdf is Jan Egil Refsnes"。

  • 上述声明的主题为:https://www.91xjr.com/rdf
  • 谓词是:作者
  • 对象是:Jan Egil Refsnes

声明:"The homepage of https://www.91xjr.com/rdf is https://www.91xjr.com"。

  • 上述声明的主题为:https://www.91xjr.com/rdf
  • 谓词是:主页
  • 对象为:https://www.91xjr.com

RDF 示例

以下是 CD 列表中的两条记录:

Title Artist Country Company Price Year
Empire Burlesque Bob Dylan USA Columbia 10.90 1985
Hide your heart Bonnie Tyler UK CBS Records 9.90 1988

下面是 RDF 文档中的几行内容:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Empire Burlesque">
  <cd:artist>Bob Dylan</cd:artist>
  <cd:country>USA</cd:country>
  <cd:company>Columbia</cd:company>
  <cd:price>10.90</cd:price>
  <cd:year>1985</cd:year>
</rdf:Description>

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Hide your heart">
  <cd:artist>Bonnie Tyler</cd:artist>
  <cd:country>UK</cd:country>
  <cd:company>CBS Records</cd:company>
  <cd:price>9.90</cd:price>
  <cd:year>1988</cd:year>
</rdf:Description>
.
.
.
</rdf:RDF>

RDF 文档的第一行是 XML 声明。 XML 声明后面是 RDF 文档的根元素:<rdf:RDF>

这个xmlns:rdf命名空间,指定带有 rdf 前缀的元素来自命名空间"http://www.w3.org/1999/02/22-rdf-syntax-ns#"。

这个xmlns:cd命名空间,指定带有 cd 前缀的元素来自命名空间 "http://www.recshop.fake/cd#"。

这个<rdf:描述>元素包含由 所标识的资源的描述rdf:关于属性。

要素:<cd:艺术家>、<cd:国家/地区>、<cd:公司>、等是资源的属性。


RDF在线验证器

W3C 的 RDF 验证服务在学习 RDF 时很有用。您可以在这里尝试 RDF 文件。

在线 RDF 验证器会解析您的 RDF 文档、检查语法并生成 RDF 文档的表格和图形视图。

将以下示例复制并粘贴到 W3C 的 RDF 验证器中:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:si="https://www.91xjr.com/rdf/">
<rdf:Description rdf:about="https://www.91xjr.com">
  <si:title>91xjr.com</si:title>
  <si:author>Jan Egil Refsnes</si:author>
</rdf:Description>
</rdf:RDF>

当你解析上面的例子时,结果看起来像这样


RDF 元素

RDF 的主要元素是根元素<RDF> 和标识资源的<Description> 元素。


<rdf:RDF> 元素

<rdf:RDF> 是 RDF 文档的根元素。它将 XML 文档定义为 RDF 文档。它还包含对 RDF 名称空间的引用:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  ... Description goes here...
</rdf:RDF>

<rdf:Description> 元素

<rdf:Description> 元素使用 about 属性标识资源。

<rdf:Description> 元素包含描述资源的元素:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Empire Burlesque">
  <cd:artist>Bob Dylan</cd:artist>
  <cd:country>USA</cd:country>
  <cd:company>Columbia</cd:company>
  <cd:price>10.90</cd:price>
  <cd:year>1985</cd:year>
</rdf:Description>

</rdf:RDF>

元素(艺术家、国家/地区、公司、价格和年份)在 http://www.recshop.fake/cd# 命名空间中定义。该名称空间位于 RDF 之外(而不是 RDF 的一部分)。 RDF 仅定义了框架。元素、艺术家、国家、公司、价格和年份必须由其他人(公司、组织、个人等)定义。


作为属性的属性

属性元素也可以定义为属性(而不是元素):

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Empire Burlesque"
cd:artist="Bob Dylan" cd:country="USA"
cd:company="Columbia" cd:price="10.90"
cd:year="1985" />

</rdf:RDF>

作为资源的属性

属性元素也可以定义为资源:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Empire Burlesque">
  <cd:artist rdf:resource="http://www.recshop.fake/cd/dylan" />
  ...
  ...
</rdf:Description>

</rdf:RDF>

在上面的示例中,属性 Artist 没有值,而是对包含艺术家信息的资源的引用。


RDF 容器

RDF 容器用于描述一组事物。

以下 RDF 元素用于描述组:<Bag>、<Seq> 和 <Alt>。


<rdf:Bag> 元素

<rdf:Bag> 元素用于描述不必按特定顺序排列的值列表。

<rdf:Bag> 元素可能包含重复值。

示例

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Beatles">
  <cd:artist>
    <rdf:Bag>
      <rdf:li>John</rdf:li>
      <rdf:li>Paul</rdf:li>
      <rdf:li>George</rdf:li>
      <rdf:li>Ringo</rdf:li>
    </rdf:Bag>
  </cd:artist>
</rdf:Description>

</rdf:RDF>

<rdf:Seq> 元素

<rdf:Seq> 元素用于描述值的有序列表(例如,按字母顺序)。

<rdf:Seq> 元素可能包含重复值。

示例

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Beatles">
  <cd:artist>
    <rdf:Seq>
      <rdf:li>George</rdf:li>
      <rdf:li>John</rdf:li>
      <rdf:li>Paul</rdf:li>
      <rdf:li>Ringo</rdf:li>
    </rdf:Seq>
  </cd:artist>
</rdf:Description>

</rdf:RDF>

<rdf:Alt> 元素

<rdf:Alt> 元素用于描述替代值列表(用户只能选择其中一个值)。

示例

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://www.recshop.fake/cd#">

<rdf:Description
rdf:about="http://www.recshop.fake/cd/Beatles">
  <cd:format>
    <rdf:Alt>
      <rdf:li>CD</rdf:li>
      <rdf:li>Record</rdf:li>
      <rdf:li>Tape</rdf:li>
    </rdf:Alt>
  </cd:format>
</rdf:Description>

</rdf:RDF>

RDF 术语

在上面的示例中,我们在描述容器元素时讨论了"list of values"。在 RDF 中,这些"list of values" 称为成员。

所以,我们有以下内容:

  • 容器是包含事物的资源
  • 包含的东西称为成员(而不是值列表)

RDF 集合

RDF 集合描述只能包含指定成员的组。


rdf:parseType="Collection" 属性

正如上一章所见,容器表示包含的资源是成员 - 它并没有说不允许其他成员。

RDF 集合用于描述只能包含指定成员的组。

集合由属性 rdf:parseType="Collection" 描述。

示例

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cd="http://recshop.fake/cd#">

<rdf:Description
rdf:about="http://recshop.fake/cd/Beatles">
  <cd:artist rdf:parseType="Collection">
    <rdf:Description rdf:about="http://recshop.fake/cd/Beatles/George"/>
    <rdf:Description rdf:about="http://recshop.fake/cd/Beatles/John"/>
    <rdf:Description rdf:about="http://recshop.fake/cd/Beatles/Paul"/>
    <rdf:Description rdf:about="http://recshop.fake/cd/Beatles/Ringo"/>
  </cd:artist>
</rdf:Description>

</rdf:RDF>

RDF 模式和应用程序类

RDF 模式 (RDFS) 是 RDF 的扩展。

RDF 用类、属性和值来描述资源。

此外,RDF 还需要一种方法来定义特定于应用程序的类和属性。特定于应用程序的类和属性必须使用 RDF 的扩展来定义。

RDF 模式就是这样的扩展之一。


RDF 模式 (RDFS)

RDF 模式不提供实际的特定于应用程序的类和属性。

相反,RDF 模式提供了描述特定于应用程序的类和属性的框架。

RDF 模式中的类非常类似于面向对象编程语言中的类。这允许将资源定义为类的实例和类的子类。


RDFS 示例

以下示例演示了一些 RDFS 设施:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.animals.fake/animals#">

<rdf:Description rdf:ID="animal">
  <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
</rdf:Description>

<rdf:Description rdf:ID="horse">
  <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  <rdfs:subClassOf rdf:resource="#animal"/>
</rdf:Description>

</rdf:RDF>

在上面的示例中,资源"horse" 是类"animal" 的子类。


示例缩写

由于 RDFS 类是 RDF 资源,我们可以使用 rdfs:Class 而不是 rdf:Description 来缩写上面的示例,并删除 rdf:type 信息:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.animals.fake/animals#">

<rdfs:Class rdf:ID="animal" />

<rdfs:Class rdf:ID="horse">
  <rdfs:subClassOf rdf:resource="#animal"/>
</rdfs:Class>

</rdf:RDF>

就是这样!


都柏林核心

都柏林核心元数据计划 (DCMI) 创建了一些用于描述文档的预定义属性。

RDF 是元数据(关于数据的数据)。 RDF用于描述信息资源。

Dublin Core 是一组用于描述文档的预定义属性。

第一个都柏林核心属性是在俄亥俄州都柏林元数据研讨会1995 年,目前由都柏林核心元数据计划

Property Definition
Contributor An entity responsible for making contributions to the content of the resource
Coverage The extent or scope of the content of the resource
Creator An entity primarily responsible for making the content of the resource
Format The physical or digital manifestation of the resource
Date A date of an event in the lifecycle of the resource
Description An account of the content of the resource
Identifier An unambiguous reference to the resource within a given context
Language A language of the intellectual content of the resource
Publisher An entity responsible for making the resource available
Relation A reference to a related resource
Rights Information about rights held in and over the resource
Source A Reference to a resource from which the present resource is derived
Subject A topic of the content of the resource
Title A name given to the resource
Type The nature or genre of the content of the resource

快速查看上表表明 RDF 非常适合表示都柏林核心信息。


RDF 示例

以下示例演示了 RDF 文档中一些 Dublin Core 属性的使用:

<?xml version="1.0"?>

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc= "http://purl.org/dc/elements/1.1/">

<rdf:Description rdf:about="https://www.91xjr.com">
  <dc:description>91xjr - Free tutorials</dc:description>
  <dc:publisher>Refsnes Data as</dc:publisher>
  <dc:date>2008-09-01</dc:date>
  <dc:type>Web Development</dc:type>
  <dc:format>text/html</dc:format>
  <dc:language>en</dc:language>
</rdf:Description>

</rdf:RDF>

RDF 参考

RDF 命名空间 (xmlns:rdf) 是:http://www.w3.org/1999/02/22-rdf-syntax-ns#

RDFS 命名空间 (xmlns:rdfs) 是:http://www.w3.org/2000/01/rdf-schema#

RDF 文件的推荐文件扩展名是.rdf。然而,扩展.xml通常用于提供与旧 xml 解析器的兼容性。

MIME 类型应该是"application/rdf+xml"


RDFS / RDF 类

Element Class of Subclass of
rdfs:Class All classes  
rdfs:Datatype Data types Class
rdfs:Resource All resources Class
rdfs:Container Containers Resource
rdfs:Literal Literal values (text and numbers) Resource
rdf:List Lists Resource
rdf:Property Properties Resource
rdf:Statement Statements Resource
rdf:Alt Containers of alternatives Container
rdf:Bag Unordered containers Container
rdf:Seq Ordered containers Container
rdfs:ContainerMembershipProperty Container membership properties Property
rdf:XMLLiteral XML literal values Literal

RDFS / RDF 属性

Element Domain Range Description
rdfs:domain Property Class The domain of the resource
rdfs:range Property Class The range of the resource
rdfs:subPropertyOf Property Property The property is a sub property of a property
rdfs:subClassOf Class Class The resource is a subclass of a class
rdfs:comment Resource Literal The human readable description of the resource
rdfs:label Resource Literal The human readable label (name)  of the resource
rdfs:isDefinedBy Resource Resource The definition of the resource
rdfs:seeAlso Resource Resource The additional information about the resource
rdfs:member Resource Resource The member of the resource
rdf:first List Resource  
rdf:rest List List  
rdf:subject Statement Resource The subject of the resource in an RDF Statement
rdf:predicate Statement Resource The predicate of the resource in an RDF Statement
rdf:object Statement Resource The object of the resource in an RDF Statement
rdf:value Resource Resource The property used for values
rdf:type Resource Class The resource is an instance of a class

RDF 属性

Attribute Description
rdf:about Defines the resource being described
rdf:Description Container for the description of a resource
rdf:resource Defines a resource to identify a property
rdf:datatype Defines the data type of an element
rdf:ID Defines the ID of an element
rdf:li Defines a list
rdf:_n Defines a node
rdf:nodeID Defines the ID of an element node
rdf:parseType Defines how an element should be parsed
rdf:RDF The root of an RDF document
xml:base Defines the XML base
xml:lang Defines the language of the element content