You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Charles Li <ch...@gmail.com> on 2013/08/31 18:36:31 UTC

How to filter SPARQL query with a list

Hi, all Jena/SPARQL experts!

I am composing a SPARQL query trying to list all triples whose subjects are
in a list of RdfId's, and this list of RdfId's are from another select
query:

select ?s  ?p  ?o
WHERE
{
   ?s  ?p  ?o
   FILTER(?s in (
                   select distinct ?s WHERE
                   {
                      ?s  ?p  ?o
                      FILTER(contains(str(?o),
"_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
                   }
                ) &&
           !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
          )
}
order by ?s


However, the SPARQL query is not well-format, and it seems that it
doesn't allow another "select" in place for the list of values.

Can someone help me and advice how I can put in a list of RdfId's from
another query (lines 6 - 10)?


Thanks a lot!

- Charles

Re: How to filter SPARQL query with a list

Posted by Andy Seaborne <an...@apache.org>.
On 02/09/13 19:06, Charles Li wrote:
> Thanks, Andy!
>
> This is a legacy file that I cannot change anything.

You are changing the file already because you are setting the base on 
loading.  Text processing can fix the file.

Or load and use SPARQL Update (slowly, but once).

> I tried using FILTER. Exactly as you said, the execution of the query is
> too slow.
>
> My questions - were you able to execute the query with URI() function in
> it?

No.

> How can I make my query execute first with no syntax error ?

 From my previous email, this may work:
[[
You can also use initial bindings (see QueryExecutionFactory) on a query
to inject values after the parse stage.
]]

	Andy

>
> Thanks for the advice - I read the book "Learning Sparql". Just need to
> read it again.
>
>
> On Mon, Sep 2, 2013 at 12:36 PM, Andy Seaborne <an...@apache.org> wrote:
>
>> On 02/09/13 16:34, Charles Li wrote
>>
>>   Thanks Andy!
>>>
>>> I managed to narrow down to the following SPAQL query issue (the
>>> model was in TDB loaded from file "C:/temp/myrdf.xml". Jena 2.10.1)
>>>
>>> When I run the following query:
>>>
>>> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#**ROOT>  ?p   ?o
>>> . }
>>>
>>> I was able to get the correct triples (the logical tree structure
>>> root is modeled in the RDF file with a node whose RdfId is literally
>>> "ROOT")  .
>>>
>>> However, when I plug in the RdfId with the following query:
>>>
>>> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#
>>> _{27900499-0D9D-4287-8F4D-**E4C13BCB3C8D}>  ?p   ?o . }
>>>
>>> I got an error:
>>>
>>> Encountered " "<" "< "" ......
>>>
>>
>> { and } are illegal in IRIs.  Ideally, your data would not use them.
>> You may run into other problems if you continue to use bad IRIs.
>>
>> You probably got a warning when loading data.
>>
>>
>>
>>> So I think it may be due to the special characters in the RdfID, and
>>> I tried the following query:
>>>
>>> select ?s  ?p  ?o WHERE {
>>>
>>>
>>> URI(ENCODE_FOR_URI("file:///C:**/temp/myrdf.xml#_{27900499-**
>>> 0D9D-4287-8F4D-E4C13BCB3C8D}")**)
>>> ?p  ?o  .
>>> }
>>>
>>
>> Encoding is not escaping.
>>
>> If you encode { as %7B then the characters in the IRI are %-7-B, it's not
>> a way like \n to get the raw character into IRI.
>>
>> You can try: CONTAINS(STR(...)) as you had before but on the correct
>> variable now we've established it is in the subject position.
>>
>> SELECT * {
>>    ?s ?p ?o
>>    FILTER( CONTAINS(STR(?s), "27900499-0D9D-4287-8F4D-**E4C13BCB3C8D")
>> }
>>
>> or
>> FILTER( STR(?s) = "file:///C:/temp/myrdf.xml#_{**0E43691F-0B64-42AF-AFCF-*
>> *19BCC28ACBA3}")
>>
>> This is not a fast query - it's a scan of the entire database - but you
>> could use it to convert your data using SPARQL Update.
>>
>> You can also use initial bindings (see QueryExecutionFactory) on a query
>> to inject values after the parse stage.
>>
>>
>>> But still syntax error.
>>>
>>
>> which is unrelated to the URI issue.  You need to look at the spec or a
>> book like "Learning SPARQL" or one of the many online introductions.
>> Guessing at syntax is going to take you a long time.
>>
>>
>>
>>   How should I compose the SPARQL query string to specify the RdfID in
>>> place correctly?
>>>
>>>
>>> Thanks a lot!
>>>
>>> - Charles
>>>
>>
>>          Andy
>>
>


Re: How to filter SPARQL query with a list

Posted by Charles Li <ch...@gmail.com>.
Thanks, Andy!

This is a legacy file that I cannot change anything.

I tried using FILTER. Exactly as you said, the execution of the query is
too slow.

My questions - were you able to execute the query with URI() function in
it? How can I make my query execute first with no syntax error ?

Thanks for the advice - I read the book "Learning Sparql". Just need to
read it again.


On Mon, Sep 2, 2013 at 12:36 PM, Andy Seaborne <an...@apache.org> wrote:

> On 02/09/13 16:34, Charles Li wrote
>
>  Thanks Andy!
>>
>> I managed to narrow down to the following SPAQL query issue (the
>> model was in TDB loaded from file "C:/temp/myrdf.xml". Jena 2.10.1)
>>
>> When I run the following query:
>>
>> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#**ROOT>  ?p   ?o
>> . }
>>
>> I was able to get the correct triples (the logical tree structure
>> root is modeled in the RDF file with a node whose RdfId is literally
>> "ROOT")  .
>>
>> However, when I plug in the RdfId with the following query:
>>
>> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#
>> _{27900499-0D9D-4287-8F4D-**E4C13BCB3C8D}>  ?p   ?o . }
>>
>> I got an error:
>>
>> Encountered " "<" "< "" ......
>>
>
> { and } are illegal in IRIs.  Ideally, your data would not use them.
> You may run into other problems if you continue to use bad IRIs.
>
> You probably got a warning when loading data.
>
>
>
>> So I think it may be due to the special characters in the RdfID, and
>> I tried the following query:
>>
>> select ?s  ?p  ?o WHERE {
>>
>>
>> URI(ENCODE_FOR_URI("file:///C:**/temp/myrdf.xml#_{27900499-**
>> 0D9D-4287-8F4D-E4C13BCB3C8D}")**)
>> ?p  ?o  .
>> }
>>
>
> Encoding is not escaping.
>
> If you encode { as %7B then the characters in the IRI are %-7-B, it's not
> a way like \n to get the raw character into IRI.
>
> You can try: CONTAINS(STR(...)) as you had before but on the correct
> variable now we've established it is in the subject position.
>
> SELECT * {
>   ?s ?p ?o
>   FILTER( CONTAINS(STR(?s), "27900499-0D9D-4287-8F4D-**E4C13BCB3C8D")
> }
>
> or
> FILTER( STR(?s) = "file:///C:/temp/myrdf.xml#_{**0E43691F-0B64-42AF-AFCF-*
> *19BCC28ACBA3}")
>
> This is not a fast query - it's a scan of the entire database - but you
> could use it to convert your data using SPARQL Update.
>
> You can also use initial bindings (see QueryExecutionFactory) on a query
> to inject values after the parse stage.
>
>
>> But still syntax error.
>>
>
> which is unrelated to the URI issue.  You need to look at the spec or a
> book like "Learning SPARQL" or one of the many online introductions.
> Guessing at syntax is going to take you a long time.
>
>
>
>  How should I compose the SPARQL query string to specify the RdfID in
>> place correctly?
>>
>>
>> Thanks a lot!
>>
>> - Charles
>>
>
>         Andy
>

Re: How to filter SPARQL query with a list

Posted by Andy Seaborne <an...@apache.org>.
On 02/09/13 16:34, Charles Li wrote
> Thanks Andy!
>
> I managed to narrow down to the following SPAQL query issue (the
> model was in TDB loaded from file "C:/temp/myrdf.xml". Jena 2.10.1)
>
> When I run the following query:
>
> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#ROOT>  ?p   ?o
> . }
>
> I was able to get the correct triples (the logical tree structure
> root is modeled in the RDF file with a node whose RdfId is literally
> "ROOT")  .
>
> However, when I plug in the RdfId with the following query:
>
> select ?s  ?p  ?o WHERE { <file:///C:/temp/myrdf.xml#
> _{27900499-0D9D-4287-8F4D-E4C13BCB3C8D}>  ?p   ?o . }
>
> I got an error:
>
> Encountered " "<" "< "" ......

{ and } are illegal in IRIs.  Ideally, your data would not use them.
You may run into other problems if you continue to use bad IRIs.

You probably got a warning when loading data.

>
> So I think it may be due to the special characters in the RdfID, and
> I tried the following query:
>
> select ?s  ?p  ?o WHERE {
>
>
> URI(ENCODE_FOR_URI("file:///C:/temp/myrdf.xml#_{27900499-0D9D-4287-8F4D-E4C13BCB3C8D}"))
>?p  ?o  .
> }

Encoding is not escaping.

If you encode { as %7B then the characters in the IRI are %-7-B, it's 
not a way like \n to get the raw character into IRI.

You can try: CONTAINS(STR(...)) as you had before but on the correct 
variable now we've established it is in the subject position.

SELECT * {
   ?s ?p ?o
   FILTER( CONTAINS(STR(?s), "27900499-0D9D-4287-8F4D-E4C13BCB3C8D")
}

or
FILTER( STR(?s) = 
"file:///C:/temp/myrdf.xml#_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}")

This is not a fast query - it's a scan of the entire database - but you 
could use it to convert your data using SPARQL Update.

You can also use initial bindings (see QueryExecutionFactory) on a query 
to inject values after the parse stage.

>
> But still syntax error.

which is unrelated to the URI issue.  You need to look at the spec or a
book like "Learning SPARQL" or one of the many online introductions.
Guessing at syntax is going to take you a long time.


> How should I compose the SPARQL query string to specify the RdfID in
> place correctly?
>
>
> Thanks a lot!
>
> - Charles

	Andy

Re: How to filter SPARQL query with a list

Posted by Charles Li <ch...@gmail.com>.
Thanks Andy!

I managed to narrow down to the following SPAQL query issue (the model was
in TDB loaded from file "C:/temp/myrdf.xml". Jena 2.10.1)

When I run the following query:

                            select ?s  ?p  ?o
                            WHERE
                            {
                               <file:///C:/temp/myrdf.xml#ROOT>  ?p   ?o .
                            }

I was able to get the correct triples (the logical tree structure root is
modeled in the RDF file with a node whose RdfId is literally "ROOT")  .

However, when I plug in the RdfId with the following query:

                            select ?s  ?p  ?o
                            WHERE
                            {
                                 <file:///C:/temp/myrdf.xml#
_{27900499-0D9D-4287-8F4D-E4C13BCB3C8D}>  ?p   ?o .
                            }

I got an error:

                         Encountered " "<" "< "" ......

So I think it may be due to the special characters in the RdfID, and I
tried the following query:

                            select ?s  ?p  ?o
                            WHERE
                            {


URI(ENCODE_FOR_URI("file:///C:/temp/myrdf.xml#_{27900499-0D9D-4287-8F4D-E4C13BCB3C8D}"))
?p  ?o  .
                }

But still syntax error.

How should I compose the SPARQL query string to specify the RdfID in
place correctly?


Thanks a lot!

- Charles



On Sun, Sep 1, 2013 at 12:21 PM, Andy Seaborne <an...@apache.org> wrote:

> On 01/09/13 16:49, Charles Li wrote:
>
>> Thanks, Andy!
>>
>> I am trying to get the exact string for the "<http://example/xyz/foo>"
>> part
>> in your message. However, the model I loaded into Jena from the RDF xml
>> doesn't have a default namespace.
>>
>> Here are my findings:
>>
>> When I print out with the following debug lines:
>>
>>                                  System.out.println("URI = " +
>> soln.get("?o").asResource().**getURI());
>> System.out.println("Local Name = " +
>> soln.get("?o").asResource().**getLocalName());
>> System.out.println("Namespace = " +
>> soln.get("?o").asResource().**getNameSpace());
>> System.out.println("Class = " + soln.get("?o").asResource().**
>> getClass());
>>
>> I got:
>>
>>                                  Namespace = file:///C:/temp/myrdf.xml#
>>                                  Class = class
>> com.hp.hpl.jena.rdf.model.**impl.ResourceImpl
>>                                  URI =
>> file:///C:/temp/myrdf.xml#_{**0E43691F-0B64-42AF-AFCF-**19BCC28ACBA3}
>>                                  Local Name =
>>
>
> This does not make sense - that output does not correspond to the debug
> lines.  The order is different.  Details matter.  Your doing something but
> it's not clear what the data is or what exactly you have done.
>
>
>  And when I tried to dump the model into a TURTLE format, I got the
>> following error:
>>
>>                                   {E211} Base URI is null, but there are
>> relative URIs to resolve.: <#_{0E43691F-0B64-42AF-AFCF-**19BCC28ACBA3}>
>>
>
> That does not look like an error from the Turtle writer.  It looks like
> it's from the RDF/XML reader as it's talking about "resolving".
>
>
>  And when I run the following SPARQL, it returned nothing:
>>
>>                                   select ?s  ?p  ?o where
>>                                   {
>>                                           ?s  ?p
>>  <file:///C:/temp/myrdf.xml/
>> _{0E43691F-0B64-42AF-AFCF-**19BCC28ACBA3}> .
>>
>
> This will not work for several reasons.
>
> 1/ rdf:ID is not a predicate - there is no triple for ?s rdf:ID ?o. rdf:ID
> will affect the subject position, not the object position.
>
> (If you spell rdf:ID wrong, it can cause objects to be created.)
>
> 2/ It's a different URI to print out above - the # has become a /.
>
>
>                                            ?s  ?p  ?o .
>>                                   }
>>
>> What did I miss?
>>
>
> Please provide a complete, minimal example and details of the version of
> the software you're using.  Descriptive fragments aren't enough for anyone
> to see what exactly you are doing.
>
> 1/ "complete" means has data and code - something someone else can run
> with little additional work, any of which migh obscure the issue
>
> 2/ "minimal" illustrates just the point in question and is triped down to
> the least needed to show what's going on.
>
> I suggest you include writing your data out as N-triples as well to see
> what the structure is.
>
>         Andy
>
>
>
>> Thanks a lot for your kindly help!!
>> - Charles
>>
>>
>> On Sun, Sep 1, 2013 at 4:30 AM, Andy Seaborne <an...@apache.org> wrote:
>>
>>  On 01/09/13 01:39, Charles Li wrote:
>>>
>>>  Thanks, Rob!
>>>>
>>>> What I really need to do is to query by a given rdf:ID string, since I
>>>> am
>>>> building an RDF/XML traversing tool. My challenge is how to quickly get
>>>> all
>>>> triples whose subject has a given RdfID.
>>>>
>>>> As you know rdf:ID is an RDF/XML artifact and there is no such a triple
>>>> in
>>>> the model whose predicate is "ID". How would up come up with a SPARQL
>>>> query
>>>> to take a string of the rdf:ID input and return all triples whose
>>>> subject
>>>> has the given RdfID?
>>>>
>>>> Any help is greatly appreciated!!!
>>>>
>>>>
>>> rdf:ID is not a predicate; it's a syntax for the subject.
>>>
>>> If you have rdf:ID="foo"
>>> and the base URI for the data is http://example/xyz/
>>> then the subject is http://example/xyz/foo.
>>>
>>> Query with
>>>
>>> <http://example/xyz/foo> ?p ?o
>>>
>>> Try writing your data out as N-triples to Turtle to see the structure and
>>> the actual URIs.
>>>
>>>          Andy
>>>
>>>
>>>
>>>  - Charles
>>>>
>>>>
>>>> On Sat, Aug 31, 2013 at 12:16 PM, Rob Vesse <rv...@yarcdata.com>
>>>> wrote:
>>>>
>>>>   There's no need to do a FILTER(?s IN sub query) and as you have found
>>>> out
>>>>
>>>>> this is actually illegal SPARQL
>>>>>
>>>>> You can however put the sub query in directly and get the desired
>>>>> effect.
>>>>> SPARQL evaluation is bottom up so the inner query gets evaluated first
>>>>>
>>>>> Rob
>>>>>
>>>>>
>>>>> ______________________________****__________
>>>>>
>>>>> From: Charles Li [charlesquanli@gmail.com]
>>>>> Sent: 31 August 2013 09:36
>>>>> To: users@jena.apache.org
>>>>> Subject: How to filter SPARQL query with a list
>>>>>
>>>>> Hi, all Jena/SPARQL experts!
>>>>>
>>>>> I am composing a SPARQL query trying to list all triples whose subjects
>>>>> are
>>>>> in a list of RdfId's, and this list of RdfId's are from another select
>>>>> query:
>>>>>
>>>>> select ?s  ?p  ?o
>>>>> WHERE
>>>>> {
>>>>>      ?s  ?p  ?o
>>>>>      FILTER(?s in (
>>>>>                      select distinct ?s WHERE
>>>>>                      {
>>>>>                         ?s  ?p  ?o
>>>>>                         FILTER(contains(str(?o),
>>>>> "_{000E9C27-150C-480B-9CC7-****E23FFC62C90A}"))
>>>>>                      }
>>>>>                   ) &&
>>>>>              !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-**
>>>>>
>>>>> E23FFC62C90A}"))
>>>>>             )
>>>>> }
>>>>> order by ?s
>>>>>
>>>>>
>>>>> However, the SPARQL query is not well-format, and it seems that it
>>>>> doesn't allow another "select" in place for the list of values.
>>>>>
>>>>> Can someone help me and advice how I can put in a list of RdfId's from
>>>>> another query (lines 6 - 10)?
>>>>>
>>>>>
>>>>> Thanks a lot!
>>>>>
>>>>> - Charles
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>

Re: How to filter SPARQL query with a list

Posted by Andy Seaborne <an...@apache.org>.
On 01/09/13 16:49, Charles Li wrote:
> Thanks, Andy!
>
> I am trying to get the exact string for the "<http://example/xyz/foo>" part
> in your message. However, the model I loaded into Jena from the RDF xml
> doesn't have a default namespace.
>
> Here are my findings:
>
> When I print out with the following debug lines:
>
>                                  System.out.println("URI = " +
> soln.get("?o").asResource().getURI());
> System.out.println("Local Name = " +
> soln.get("?o").asResource().getLocalName());
> System.out.println("Namespace = " +
> soln.get("?o").asResource().getNameSpace());
> System.out.println("Class = " + soln.get("?o").asResource().getClass());
>
> I got:
>
>                                  Namespace = file:///C:/temp/myrdf.xml#
>                                  Class = class
> com.hp.hpl.jena.rdf.model.impl.ResourceImpl
>                                  URI =
> file:///C:/temp/myrdf.xml#_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}
>                                  Local Name =

This does not make sense - that output does not correspond to the debug 
lines.  The order is different.  Details matter.  Your doing something 
but it's not clear what the data is or what exactly you have done.

> And when I tried to dump the model into a TURTLE format, I got the
> following error:
>
>                                   {E211} Base URI is null, but there are
> relative URIs to resolve.: <#_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}>

That does not look like an error from the Turtle writer.  It looks like 
it's from the RDF/XML reader as it's talking about "resolving".

> And when I run the following SPARQL, it returned nothing:
>
>                                   select ?s  ?p  ?o where
>                                   {
>                                           ?s  ?p  <file:///C:/temp/myrdf.xml/
> _{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}> .

This will not work for several reasons.

1/ rdf:ID is not a predicate - there is no triple for ?s rdf:ID ?o. 
rdf:ID will affect the subject position, not the object position.

(If you spell rdf:ID wrong, it can cause objects to be created.)

2/ It's a different URI to print out above - the # has become a /.

>                                           ?s  ?p  ?o .
>                                   }
>
> What did I miss?

Please provide a complete, minimal example and details of the version of 
the software you're using.  Descriptive fragments aren't enough for 
anyone to see what exactly you are doing.

1/ "complete" means has data and code - something someone else can run 
with little additional work, any of which migh obscure the issue

2/ "minimal" illustrates just the point in question and is triped down 
to the least needed to show what's going on.

I suggest you include writing your data out as N-triples as well to see 
what the structure is.

	Andy


>
> Thanks a lot for your kindly help!!
> - Charles
>
>
> On Sun, Sep 1, 2013 at 4:30 AM, Andy Seaborne <an...@apache.org> wrote:
>
>> On 01/09/13 01:39, Charles Li wrote:
>>
>>> Thanks, Rob!
>>>
>>> What I really need to do is to query by a given rdf:ID string, since I am
>>> building an RDF/XML traversing tool. My challenge is how to quickly get
>>> all
>>> triples whose subject has a given RdfID.
>>>
>>> As you know rdf:ID is an RDF/XML artifact and there is no such a triple in
>>> the model whose predicate is "ID". How would up come up with a SPARQL
>>> query
>>> to take a string of the rdf:ID input and return all triples whose subject
>>> has the given RdfID?
>>>
>>> Any help is greatly appreciated!!!
>>>
>>
>> rdf:ID is not a predicate; it's a syntax for the subject.
>>
>> If you have rdf:ID="foo"
>> and the base URI for the data is http://example/xyz/
>> then the subject is http://example/xyz/foo.
>>
>> Query with
>>
>> <http://example/xyz/foo> ?p ?o
>>
>> Try writing your data out as N-triples to Turtle to see the structure and
>> the actual URIs.
>>
>>          Andy
>>
>>
>>
>>> - Charles
>>>
>>>
>>> On Sat, Aug 31, 2013 at 12:16 PM, Rob Vesse <rv...@yarcdata.com> wrote:
>>>
>>>   There's no need to do a FILTER(?s IN sub query) and as you have found out
>>>> this is actually illegal SPARQL
>>>>
>>>> You can however put the sub query in directly and get the desired effect.
>>>> SPARQL evaluation is bottom up so the inner query gets evaluated first
>>>>
>>>> Rob
>>>>
>>>>
>>>> ______________________________**__________
>>>> From: Charles Li [charlesquanli@gmail.com]
>>>> Sent: 31 August 2013 09:36
>>>> To: users@jena.apache.org
>>>> Subject: How to filter SPARQL query with a list
>>>>
>>>> Hi, all Jena/SPARQL experts!
>>>>
>>>> I am composing a SPARQL query trying to list all triples whose subjects
>>>> are
>>>> in a list of RdfId's, and this list of RdfId's are from another select
>>>> query:
>>>>
>>>> select ?s  ?p  ?o
>>>> WHERE
>>>> {
>>>>      ?s  ?p  ?o
>>>>      FILTER(?s in (
>>>>                      select distinct ?s WHERE
>>>>                      {
>>>>                         ?s  ?p  ?o
>>>>                         FILTER(contains(str(?o),
>>>> "_{000E9C27-150C-480B-9CC7-**E23FFC62C90A}"))
>>>>                      }
>>>>                   ) &&
>>>>              !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-**
>>>> E23FFC62C90A}"))
>>>>             )
>>>> }
>>>> order by ?s
>>>>
>>>>
>>>> However, the SPARQL query is not well-format, and it seems that it
>>>> doesn't allow another "select" in place for the list of values.
>>>>
>>>> Can someone help me and advice how I can put in a list of RdfId's from
>>>> another query (lines 6 - 10)?
>>>>
>>>>
>>>> Thanks a lot!
>>>>
>>>> - Charles
>>>>
>>>>
>>>
>>
>


Re: How to filter SPARQL query with a list

Posted by Charles Li <ch...@gmail.com>.
Thanks, Andy!

I am trying to get the exact string for the "<http://example/xyz/foo>" part
in your message. However, the model I loaded into Jena from the RDF xml
doesn't have a default namespace.

Here are my findings:

When I print out with the following debug lines:

                                System.out.println("URI = " +
soln.get("?o").asResource().getURI());
System.out.println("Local Name = " +
soln.get("?o").asResource().getLocalName());
System.out.println("Namespace = " +
soln.get("?o").asResource().getNameSpace());
System.out.println("Class = " + soln.get("?o").asResource().getClass());

I got:

                                Namespace = file:///C:/temp/myrdf.xml#
                                Class = class
com.hp.hpl.jena.rdf.model.impl.ResourceImpl
                                URI =
file:///C:/temp/myrdf.xml#_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}
                                Local Name =

And when I tried to dump the model into a TURTLE format, I got the
following error:

                                 {E211} Base URI is null, but there are
relative URIs to resolve.: <#_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}>

And when I run the following SPARQL, it returned nothing:

                                 select ?s  ?p  ?o where
                                 {
                                         ?s  ?p  <file:///C:/temp/myrdf.xml/
_{0E43691F-0B64-42AF-AFCF-19BCC28ACBA3}> .
                                         ?s  ?p  ?o .
                                 }

What did I miss?

Thanks a lot for your kindly help!!
- Charles


On Sun, Sep 1, 2013 at 4:30 AM, Andy Seaborne <an...@apache.org> wrote:

> On 01/09/13 01:39, Charles Li wrote:
>
>> Thanks, Rob!
>>
>> What I really need to do is to query by a given rdf:ID string, since I am
>> building an RDF/XML traversing tool. My challenge is how to quickly get
>> all
>> triples whose subject has a given RdfID.
>>
>> As you know rdf:ID is an RDF/XML artifact and there is no such a triple in
>> the model whose predicate is "ID". How would up come up with a SPARQL
>> query
>> to take a string of the rdf:ID input and return all triples whose subject
>> has the given RdfID?
>>
>> Any help is greatly appreciated!!!
>>
>
> rdf:ID is not a predicate; it's a syntax for the subject.
>
> If you have rdf:ID="foo"
> and the base URI for the data is http://example/xyz/
> then the subject is http://example/xyz/foo.
>
> Query with
>
> <http://example/xyz/foo> ?p ?o
>
> Try writing your data out as N-triples to Turtle to see the structure and
> the actual URIs.
>
>         Andy
>
>
>
>> - Charles
>>
>>
>> On Sat, Aug 31, 2013 at 12:16 PM, Rob Vesse <rv...@yarcdata.com> wrote:
>>
>>  There's no need to do a FILTER(?s IN sub query) and as you have found out
>>> this is actually illegal SPARQL
>>>
>>> You can however put the sub query in directly and get the desired effect.
>>> SPARQL evaluation is bottom up so the inner query gets evaluated first
>>>
>>> Rob
>>>
>>>
>>> ______________________________**__________
>>> From: Charles Li [charlesquanli@gmail.com]
>>> Sent: 31 August 2013 09:36
>>> To: users@jena.apache.org
>>> Subject: How to filter SPARQL query with a list
>>>
>>> Hi, all Jena/SPARQL experts!
>>>
>>> I am composing a SPARQL query trying to list all triples whose subjects
>>> are
>>> in a list of RdfId's, and this list of RdfId's are from another select
>>> query:
>>>
>>> select ?s  ?p  ?o
>>> WHERE
>>> {
>>>     ?s  ?p  ?o
>>>     FILTER(?s in (
>>>                     select distinct ?s WHERE
>>>                     {
>>>                        ?s  ?p  ?o
>>>                        FILTER(contains(str(?o),
>>> "_{000E9C27-150C-480B-9CC7-**E23FFC62C90A}"))
>>>                     }
>>>                  ) &&
>>>             !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-**
>>> E23FFC62C90A}"))
>>>            )
>>> }
>>> order by ?s
>>>
>>>
>>> However, the SPARQL query is not well-format, and it seems that it
>>> doesn't allow another "select" in place for the list of values.
>>>
>>> Can someone help me and advice how I can put in a list of RdfId's from
>>> another query (lines 6 - 10)?
>>>
>>>
>>> Thanks a lot!
>>>
>>> - Charles
>>>
>>>
>>
>

Re: How to filter SPARQL query with a list

Posted by Andy Seaborne <an...@apache.org>.
On 01/09/13 01:39, Charles Li wrote:
> Thanks, Rob!
>
> What I really need to do is to query by a given rdf:ID string, since I am
> building an RDF/XML traversing tool. My challenge is how to quickly get all
> triples whose subject has a given RdfID.
>
> As you know rdf:ID is an RDF/XML artifact and there is no such a triple in
> the model whose predicate is "ID". How would up come up with a SPARQL query
> to take a string of the rdf:ID input and return all triples whose subject
> has the given RdfID?
>
> Any help is greatly appreciated!!!

rdf:ID is not a predicate; it's a syntax for the subject.

If you have rdf:ID="foo"
and the base URI for the data is http://example/xyz/
then the subject is http://example/xyz/foo.

Query with

<http://example/xyz/foo> ?p ?o

Try writing your data out as N-triples to Turtle to see the structure 
and the actual URIs.

	Andy

>
> - Charles
>
>
> On Sat, Aug 31, 2013 at 12:16 PM, Rob Vesse <rv...@yarcdata.com> wrote:
>
>> There's no need to do a FILTER(?s IN sub query) and as you have found out
>> this is actually illegal SPARQL
>>
>> You can however put the sub query in directly and get the desired effect.
>> SPARQL evaluation is bottom up so the inner query gets evaluated first
>>
>> Rob
>>
>>
>> ________________________________________
>> From: Charles Li [charlesquanli@gmail.com]
>> Sent: 31 August 2013 09:36
>> To: users@jena.apache.org
>> Subject: How to filter SPARQL query with a list
>>
>> Hi, all Jena/SPARQL experts!
>>
>> I am composing a SPARQL query trying to list all triples whose subjects are
>> in a list of RdfId's, and this list of RdfId's are from another select
>> query:
>>
>> select ?s  ?p  ?o
>> WHERE
>> {
>>     ?s  ?p  ?o
>>     FILTER(?s in (
>>                     select distinct ?s WHERE
>>                     {
>>                        ?s  ?p  ?o
>>                        FILTER(contains(str(?o),
>> "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
>>                     }
>>                  ) &&
>>             !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
>>            )
>> }
>> order by ?s
>>
>>
>> However, the SPARQL query is not well-format, and it seems that it
>> doesn't allow another "select" in place for the list of values.
>>
>> Can someone help me and advice how I can put in a list of RdfId's from
>> another query (lines 6 - 10)?
>>
>>
>> Thanks a lot!
>>
>> - Charles
>>
>


Re: How to filter SPARQL query with a list

Posted by Charles Li <ch...@gmail.com>.
Thanks, Rob!

What I really need to do is to query by a given rdf:ID string, since I am
building an RDF/XML traversing tool. My challenge is how to quickly get all
triples whose subject has a given RdfID.

As you know rdf:ID is an RDF/XML artifact and there is no such a triple in
the model whose predicate is "ID". How would up come up with a SPARQL query
to take a string of the rdf:ID input and return all triples whose subject
has the given RdfID?

Any help is greatly appreciated!!!

- Charles


On Sat, Aug 31, 2013 at 12:16 PM, Rob Vesse <rv...@yarcdata.com> wrote:

> There's no need to do a FILTER(?s IN sub query) and as you have found out
> this is actually illegal SPARQL
>
> You can however put the sub query in directly and get the desired effect.
> SPARQL evaluation is bottom up so the inner query gets evaluated first
>
> Rob
>
>
> ________________________________________
> From: Charles Li [charlesquanli@gmail.com]
> Sent: 31 August 2013 09:36
> To: users@jena.apache.org
> Subject: How to filter SPARQL query with a list
>
> Hi, all Jena/SPARQL experts!
>
> I am composing a SPARQL query trying to list all triples whose subjects are
> in a list of RdfId's, and this list of RdfId's are from another select
> query:
>
> select ?s  ?p  ?o
> WHERE
> {
>    ?s  ?p  ?o
>    FILTER(?s in (
>                    select distinct ?s WHERE
>                    {
>                       ?s  ?p  ?o
>                       FILTER(contains(str(?o),
> "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
>                    }
>                 ) &&
>            !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
>           )
> }
> order by ?s
>
>
> However, the SPARQL query is not well-format, and it seems that it
> doesn't allow another "select" in place for the list of values.
>
> Can someone help me and advice how I can put in a list of RdfId's from
> another query (lines 6 - 10)?
>
>
> Thanks a lot!
>
> - Charles
>

RE: How to filter SPARQL query with a list

Posted by Rob Vesse <rv...@yarcdata.com>.
There's no need to do a FILTER(?s IN sub query) and as you have found out this is actually illegal SPARQL

You can however put the sub query in directly and get the desired effect. SPARQL evaluation is bottom up so the inner query gets evaluated first

Rob


________________________________________
From: Charles Li [charlesquanli@gmail.com]
Sent: 31 August 2013 09:36
To: users@jena.apache.org
Subject: How to filter SPARQL query with a list

Hi, all Jena/SPARQL experts!

I am composing a SPARQL query trying to list all triples whose subjects are
in a list of RdfId's, and this list of RdfId's are from another select
query:

select ?s  ?p  ?o
WHERE
{
   ?s  ?p  ?o
   FILTER(?s in (
                   select distinct ?s WHERE
                   {
                      ?s  ?p  ?o
                      FILTER(contains(str(?o),
"_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
                   }
                ) &&
           !(contains(str(?o), "_{000E9C27-150C-480B-9CC7-E23FFC62C90A}"))
          )
}
order by ?s


However, the SPARQL query is not well-format, and it seems that it
doesn't allow another "select" in place for the list of values.

Can someone help me and advice how I can put in a list of RdfId's from
another query (lines 6 - 10)?


Thanks a lot!

- Charles