如何在 sparql 中使用 kleene 星号运算符 (*) 或其变体 (+)?
How to use a kleene star operator (*) or it's variant (+) with variables in sparql?
我有一些工作代码可以在层次结构中获取术语的所有祖先。
正在关注:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)
当 IRI 的更广泛谓词是子属性时它会中断 (?p rdf:subPropertyOf skos:broader
)
所以现在我这样做是为了捕获所有子属性谓词:
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)
我真正想做的是:
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)
但使用 ?p+
或 ?p*
会引发错误。
Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>
如何将 *
/+
与变量一起使用?
你不能。正如 SPARQL 1.1 规范的 Property Paths section 所述:
The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.
您可以使用替代方法来捕获此内容:
?parent (skos:broader|your:alternative)* ?grandparent
确切的形式将需要反映您的数据结构以及您是否要允许混合 skos:broader
和您的替代方案(我的示例允许)。如果您想要特定属性的纯链,可以将 *
运算符移到括号内并将其添加到每个替代项中。
我有一些工作代码可以在层次结构中获取术语的所有祖先。 正在关注:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)
当 IRI 的更广泛谓词是子属性时它会中断 (?p rdf:subPropertyOf skos:broader
)
所以现在我这样做是为了捕获所有子属性谓词:
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)
我真正想做的是:
select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)
但使用 ?p+
或 ?p*
会引发错误。
Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>
如何将 *
/+
与变量一起使用?
你不能。正如 SPARQL 1.1 规范的 Property Paths section 所述:
The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.
您可以使用替代方法来捕获此内容:
?parent (skos:broader|your:alternative)* ?grandparent
确切的形式将需要反映您的数据结构以及您是否要允许混合 skos:broader
和您的替代方案(我的示例允许)。如果您想要特定属性的纯链,可以将 *
运算符移到括号内并将其添加到每个替代项中。