How do I retrieve variables from an OPTIONAL statement in SPARQL?
<p>If I give my SPARQL query a known item which could be a number of different types. Depending on which type I'm currently interested in I want to get a certain property from this type (the way to do this will be different for each type) but once I have found this property I want to perform the same operation. </p>

<p>currently I have the following pseudocode:</p>

<pre><code>?object rdf:hasProperty "known Property Type"
OPTIONAL {
?object rdf:hasProperty "property type 1"
#do this thing and store thing of interest in ?variableOfInterest
}
OPTIONAL{
?object rdf:hasProperty "property type 2"
#do different thing and store thing of interest in ?variableOfInterest
}
?thingIAmActuallyInterestedIn rdf:has type ?variableOfInterest
#now do long query
</code></pre>

<p>My problem is that outside of the OPTIONAL statement <code>?variableOfInterest</code> does not get passed out, instead <code>?thingIAmActuallyInterestedIn</code> is just a list off all objects.</p>

<p>I could put the 'long query' in both of the optional blocks but that would be a huge amount of code replication.</p>

<p>Is there a way to output the <code>?variableOfInterest</code> from the optional statement rather than it being a dummy variable? </p>