我的问题是我想检索所有包含 cim:ACLineSegment的主题,无论他们可能拥有什么 ID。
<cim:ACLineSegment rdf:ID="_05b8">
<!-- some code... -->
</cim:ACLineSegment>
您的主题不“包含” cim:ACLineSegment
。主题在 RDF 中不包含任何内容。资源(包括三元组的主题)由 URI 标识。RDF 基于{subject, predicate, object} 形式的三元组。您显示的片段是 RDF/XML,它是三元组的特定序列化。在您显示的代码段中,有一些三元组的形式{<.../_05b8>, rdf:type, cim:ACLineSegment}
。也就是说,您的主题具有cim:ACLineSegment
作为rdf:type
属性值的值。
为了解释更多,假设您有以下数据。(顺便说一句,将来,如果您提供最少但完整的示例数据,这将更有帮助。您提供的数据不完整,我们无法对其运行查询,我们也不能确定它在上下文中是什么。)
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ex="http://stackoverflow.com/q/23432445/1281433/ex#"
xml:base="http://stackoverflow.com/q/23432445/1281433/ex#">
<ex:Person rdf:ID="Mary">
<ex:hasName>Mary</ex:hasName>
</ex:Person>
<ex:Person rdf:ID="Jim">
<ex:hasName>James</ex:hasName>
<ex:hasAge rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">34</ex:hasAge>
</ex:Person>
<ex:Person rdf:ID="John">
<ex:hasName>John</ex:hasName>
</ex:Person>
</rdf:RDF>
如果您查看 N-Triples 序列化中的这些数据(每行只有一个三元组),您会看到这些三元组:
<http://stackoverflow.com/q/23432445/1281433/ex#Mary> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://stackoverflow.com/q/23432445/1281433/ex#Person> .
<http://stackoverflow.com/q/23432445/1281433/ex#Mary> <http://stackoverflow.com/q/23432445/1281433/ex#hasName> "Mary" .
<http://stackoverflow.com/q/23432445/1281433/ex#John> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://stackoverflow.com/q/23432445/1281433/ex#Person> .
<http://stackoverflow.com/q/23432445/1281433/ex#John> <http://stackoverflow.com/q/23432445/1281433/ex#hasName> "John" .
<http://stackoverflow.com/q/23432445/1281433/ex#Jim> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://stackoverflow.com/q/23432445/1281433/ex#Person> .
<http://stackoverflow.com/q/23432445/1281433/ex#Jim> <http://stackoverflow.com/q/23432445/1281433/ex#hasName> "James" .
<http://stackoverflow.com/q/23432445/1281433/ex#Jim> <http://stackoverflow.com/q/23432445/1281433/ex#hasAge> "34"^^<http://www.w3.org/2001/XMLSchema#integer> .
注意到所有这些rdf:type
三元组了吗?这是因为在 RDF/XML 语法(第 2.13 节类型化节点元素)中,对应于资源的元素具有元素名称作为rdf:type
. 因此,上面的 RDF/XML 实际上是该数据的简写:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ex="http://stackoverflow.com/q/23432445/1281433/ex#" >
<rdf:Description rdf:about="http://stackoverflow.com/q/23432445/1281433/ex#Mary">
<rdf:type rdf:resource="http://stackoverflow.com/q/23432445/1281433/ex#Person"/>
<ex:hasName>Mary</ex:hasName>
</rdf:Description>
<rdf:Description rdf:about="http://stackoverflow.com/q/23432445/1281433/ex#Jim">
<rdf:type rdf:resource="http://stackoverflow.com/q/23432445/1281433/ex#Person"/>
<ex:hasName>James</ex:hasName>
<ex:hasAge rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">34</ex:hasAge>
</rdf:Description>
<rdf:Description rdf:about="http://stackoverflow.com/q/23432445/1281433/ex#John">
<rdf:type rdf:resource="http://stackoverflow.com/q/23432445/1281433/ex#Person"/>
<ex:hasName>John</ex:hasName>
</rdf:Description>
</rdf:RDF>
以 N3 或 Turtle 语法查看数据也很有帮助,因为它更接近 SPARQL 查询语法:
@prefix ex: <http://stackoverflow.com/q/23432445/1281433/ex#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:Mary a ex:Person ;
ex:hasName "Mary" .
ex:John a ex:Person ;
ex:hasName "John" .
ex:Jim a ex:Person ;
ex:hasAge 34 ;
ex:hasName "James" .
所有这一切意味着你只是想请求具有指定类型的东西。您的查询将是:
select ?predicate ?object where {
?subject rdf:type cim:ACLineSegment ; # You can use `a` instead of rdf:type.
?predicate ?object
}