XSLT 是为处理 XML 节点树而设计的。虽然有一些 RDF 序列化是 XML 节点的“树”(RDF/XML 和 RDF/XML-Abbrev),但底层 RDF 数据模型是一个图。
如果您生成的 RDF 图也不是树,那么您将不得不在 XSLT 中做一些肮脏的事情来遍历引用,并且性能/可维护性/健全性可能会受到影响。如果您修改 OWL 格式然后想转换回非 RDF XML,请注意这一点。
一个简单的(树)例子如下:
## Foo has two types
@prefix e: <uri://example#>.
e:Foo a e:Bar.
e:Foo a e:Baz. # Second statement about e:Foo
对于转换回非 RDF XML,如果您使用最基本的 RDF/XML 表单,您将在顶级 rdf:RDF 元素下立即获得 RDF 语句列表。转换这些可能涉及一遍又一遍地搜索整个语句列表。
<rdf:RDF xmlns:e="uri://example#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="uri://example#Foo">
<rdf:type rdf:resource="uri://example#Baz"/>
</rdf:Description>
<rdf:Description rdf:about="uri://example#Foo">
<rdf:type rdf:resource="uri://example#Bar"/>
</rdf:Description>
</rdf:RDF>
您可能会发现 RDF/XML-Abbrev 格式更易于阅读,但使用 XSLT 处理它并不容易,因为 RDF 的数据模型是无序的,并且一张图可以有许多等效的(但与您的 XSLT 不兼容的)XML 表单。上面的例子可以序列化为以下任意一种:
<!-- Bar is the containing element -->
<rdf:RDF xmlns:e="uri://example#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<e:Bar rdf:about="uri://example#Foo">
<rdf:type rdf:resource="uri://example#Baz"/>
</e:Bar>
</rdf:RDF>
<!-- Baz is the containing element -->
<rdf:RDF xmlns:e="uri://example#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<e:Baz rdf:about="uri://example#Foo">
<rdf:type rdf:resource="uri://example#Bar"/>
</e:Bar>
</rdf:RDF>
Pete Kirkham 提出的为序列化创建规范形式的建议将有助于您编写 XSLT。在大多数情况下,给定完全相同的输入,RDF 库每次都会将语句序列化为相同的格式,但从长远来看,我不会依赖这一点,因为 RDF 图中的数据是无序的。