I can't change output of CONSTRUCT Query - concat doesn't work as expected

82 views Asked by At

I use Protege v5.5.0 to create a query from RDF/XML file with the content:


<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:some="http://some.org/#"
         xml:base="http://some.org/">

<some:builder rdf:about="#Builder1">
<some:built rdf:resource="#Building1"/>
<some:built rdf:resource="#Building2"/>
<rdf:type rdf:resource="#building"/>
</some:builder>


<some:builder rdf:about="#Builder2">
<some:built rdf:resource="#Building3"/>
</some:builder >

</rdf:RDF>

I am using a CONSTRUCT query like this:

PREFIX art: <http://some.org/#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT   {?y some:wasBuiltBy ?x
}
WHERE       { ?x some:built ?y
}

The output looks like this:

building1   some:wasBuiltBy     Builder1 
building2   some:wasBuiltBy     Builder2 
building3   some:wasBuiltBy     Builder3 

But expected output must be like this:

 some:building1   some:wasBuiltBy     some:Builder1 
 some:building2   some:wasBuiltBy     some:Builder2 
 some:building3   some:wasBuiltBy     some:Builder3 

The task is to add to the outputted subjects and objects the prefix "some:". I tried to use CONCAT:

PREFIX art: <http://some.org/#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT   {(CONCAT("some:", ?y) as ?y) some:wasBuiltBy (CONCAT("some:", ?x) as ?x)
}
WHERE       { ?x some:built ?y
}

but it returned an error. I would welcome any help. Thank you.

1

There are 1 answers

5
Valerio Cocchi On

There are a couple of issues with your query:

1- You need to bind the variables in the WHERE clause and not in the CONSTRUCT. So, you'll need:

PREFIX art: <http://some.org/#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT { ?newY some:wasBuiltBy ?newX }
WHERE {
?x some:built ?y .
BIND(CONCAT("some:", ?y) AS ?newY)
BIND(CONCAT("some:", ?x) as ?newX)
}

2- Prefixes are for IRIs, not strings. So you'll need to account for this, as below:

PREFIX art: <http://some.org/#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

CONSTRUCT { ?newY some:wasBuiltBy ?newX }
WHERE {
?x some:built ?y .
BIND(IRI(CONCAT(STR(some:), STR(?y))) AS ?newY)
BIND(IRI(CONCAT(STR(some:), STR(?x)) as ?newX)
}

This will ensure first that ?x and ?y are actually strings, which they may not be.

Then, you will concatenate the prefix some:, i.e. "http://some.prefix/etc#", rather than the string "some:".

Finally, you will convert the whole concatenation to an IRI.