linked data - "if" in a SPARQL query -


my problem simple: have source my:g1 contains:

my:a1 my:b "literal 1" 

then have second source my:g2 contains:

my:a2 my:b my:c. my:c rdfs:label "literal 2" 

how can set sparql query produces like:

| ?a    | ?b    | ?literal    | |-------|-------|-------------| | my:a1 | my:b  | "literal 1" | | my:a2 | my:b  | "literal 2" | 

ie. how can tell sparql use same variable both "literal 1" , "literal 2": i'm looking like

select ?a ?b ?literal  {  if (?g = my:g1)   graph ?g { ?a ?b ?literal}  else if (?g = my:g2)   graph ?g { ?a ?b ?c. ?c rdfs:label ?literal} } 

note: know query horribly wrong, clarify intention

edit:

in specific case "union" statement

select ?a ?b ?literal  { {  graph my:g1 { ?a ?b ?literal} } union {   graph my:g2 { ?a ?b ?c. ?c rdfs:label ?literal} } } 

would work, not "real" case. there other solutions?

you can use property path , filter. trick here property path question mark means path of length 0 or 1. if path of length 0, ?literal same ?c, covers case when ?a related directly literal. if path length 1, ?literal value of rdfs:label ?c.

here's example real data:

@prefix : <urn:ex:>  :a :b "literal 1" . :a :b :c . :c :label "literal 2" .prefix : <urn:ex:> 
select distinct ?a ?b ?literal {   ?a ?b ?c .   ?c :label? ?literal   filter isliteral(?literal) } 
----------------------------- |  | b      | literal     | ============================= | :a | :b     | "literal 1" | | :a | :b     | "literal 2" | | :c | :label | "literal 2" | ----------------------------- 

you might not have been expecting last row in results, if ?a , ?b variables, makes sense, because there's nothing saying variable ?b has bound specific property :b.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -