SPARQL若干问题的解释 刘颖颖 2012.09.24
基本语法 SelectQuery ::= SelectClause DatasetClause* Wh ereClause SolutionModifier SelectClause ::= 'SELECT' ( 'DISTINCT' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ | '*' ) DatasetClause ::= 'FROM' ( DefaultGraphClause | NamedGraphCl ause ) WhereClause ::= 'WHERE'? GroupGraphPattern SolutionModifier ::= GroupClause? HavingClause? OrderClause? LimitOffsetClauses? Select distinct ?X From <IRI> Where{……} Group by Having
whereclause WhereClause ::= 'WHERE'? GroupGraphPatter n GroupGraphPattern ::= '{' ( SubSelect | GroupGraphP atternSub ) '}‘ GroupGraphPatternSub ::= TriplesBlock? ( GraphPatternNotTriples '.'? TriplesBlock? )* GraphPatternNotTriples ::= GroupOrUnionGraphPa ttern | OptionalGraphPattern | MinusGraphPattern | GraphGra phPattern | ServiceGraphPattern | Filter
子查询 在某些复杂的SPARQL查询语句中,会出现子查询,与 SQL一样,子查询即在where{}中嵌套另一个完整的查 询语句,例如: SELECT ?X,?Y WHERE { ?X …… SELECT ?Y WHERE{ ?Y …… }
Subselect语法 WhereClause ::= 'WHERE'? GroupGraphPatter n GroupGraphPattern ::= '{' ( SubSelect | GroupGraphP atternSub ) '}‘ SubSelect ::= SelectClause WhereClause Solu tionModifier
联合查询 基于图模式的联合查询,对每个图模式都进行匹配,合 并单独查询的结果形成结果集。相当于 OR 运算符。 SELECT ?title ?author WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author } UNION { ?book dc11:title ?title . ?book dc11:creator ?author } } 基于图模式的联合查询,对每个图模式都进行匹配,合 并单独查询的结果形成结果集。相当于 OR 运算符。 GraphPatternNotTriples ::= GroupOrUnionGraphPatter n | OptionalGraphPattern | MinusGraphPattern | GraphGraphP attern | ServiceGraphPattern | Filter GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )*
OPTIONAL OPTIONAL{}表示,括号中的图模式是可选的,不是必 须要满足匹配的条件。 如: SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } GraphPatternNotTriples ::= GroupOrUnionGraphPatter n | OptionalGraphPattern | MinusGraphPattern | GraphGraphPatte rn | ServiceGraphPattern | Filter OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPatter n
FILTER语法 Filter ::= 'FILTER' BuiltInCall | FunctionCall GraphPatternNotTriples ::= GroupOrUnionGraph Pattern | OptionalGraphPattern | MinusGraphPattern | Gr aphGraphPattern | ServiceGraphPattern | Filter Filter ::= 'FILTER' BuiltInCall | FunctionCall BuiltInCall ::= RegexExpression | ExistsFunc | NotExistsFunc RegexExpression ::= 'REGEX' '(' Expression ',' Exp ression ( ',' Expression )? ')‘ NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern ExistsFunc ::= 'EXISTS' GroupGraphPattern
否定语句——FILTER否定 PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:givenName ?name . FILTER NOT EXISTS { ?x foaf:knows ?y } ?x foaf:knows ?who } 表示查找不满足图模式的数据。
MINUS语法 MinusGraphPattern ::= 'MINUS' GroupGraphPattern GraphPatternNotTriples ::= GroupOrUnionGraph Pattern | OptionalGraphPattern | MinusGraphPattern | Gr aphGraphPattern | ServiceGraphPattern | Filter MinusGraphPattern ::= 'MINUS' GroupGraphPattern
否定语句——MINUS否定 判断MINUS左边的图模式数据集与右边的图模式是 否匹配,如果与否定条件匹配,则过滤此结果。如: WHERE { {?s ?p ?o .} MINUS { ?s foaf:givenName "Bob" } } 表示在满足{?s ?p ?o}条件的三元组中,过滤掉满足 {?S foaf:givenName “Bob”}条件的三元组。
END