I would like to query a Fedora 3.8.1-based Mulgara triple store and apply an EXSLT string function to the results. The Mulgara documentation[1,2] suggests that the EXSLT[3] functions are builtin, but I'm not sure about the required syntax for accessing them. I've tried declaring the EXSLT namespace as a PREFIX, using the default QName representation, as well as using the full namespace as a URI. The following query is as close as I've been able to get, but it only returns false.
Any advice or suggestions would be greatly appreciated. Thanks for your help.
PREFIX fedora-view: <info:fedora/fedora-system:def/view#>
PREFIX exsl-str: <http://exslt.org/strings>
SELECT ?distincts
FROM <#ri>
WHERE {
?pid fedora-view:disseminates ?dsids .
LET ( ?distincts := str:replace(str(?dsids), "^(info:fedora).*$", "$1"))
}
In this contrived example, the returns would look like:
info:fedora
info:fedora
info:fedora
instead of:
info:fedora/pid:1/TN
info:fedora/pid:35/TECHMD
info:fedora/pid:46/RELS-EXT
[1] https://code.mulgara.org/projects/mulgara/wiki/Functions
[2] https://code.mulgara.org/projects/mulgara/wiki/SPARQLExt
[3] http://exslt.org/
Related
the problem is that I need to extract data from xml, I know how to extract them, but I can't pass prefixes, maybe you can help
<ns2:PositiveInfo xmlns:ns2="http://ws.nGCR">
<BatchResponse xmlns="http://katana">
<Header>
<BatchId>11480644</BatchId>
<State>Finished</State>
<BeginTimeStamp>2022-09-10T10:21:48Z</BeginTimeStamp>
<TimeStamp>2022-09-10T10:21:50Z</TimeStamp>
<FinishTimeStamp>2022-09-10T10:21:50Z</FinishTimeStamp>
<Duration>2.3571</Duration>
<Identifier>600e19f5cc5b4707944b126cc8f6103a</Identifier>
<Subscriber>2810192</Subscriber>
.....
Now im in PositiveInfo prefix, how to reach BatchResponse prefix?
until now I have this query :
select *
from(
select unnest(xpath('/responseContainer/ns2:Report/ns2:Registers/ns2:PositiveInfo',
response_body::XML,
array[array['ns2','http://ws.nGCR']]))::XML as test
from stage_lt.cb_data_execution_entry_details deed
where id = 178752351)xx
To select the BatchResponse element, whose name is in the http://katana namespace, you'll need to bind another prefix (e.g. katana) to the namespace URI http://katana, just as you have bound the ns2 prefix to the namespace URI http://ws.nGCR, then you can use katana: as a namespace prefix in your XPath expression, e.g. /responseContainer/ns2:Report/ns2:Registers/ns2:PositiveInfo/katana:BatchResponse
I've been trying (with no success so far) to filter values with a "broader equals" condition. That is, ignoring diacritics.
select * where {
?s per:surname1 ?t.
bind (fn:starts-with(str(?t),'Maria') as ?noAccent1) .
bind (fn:translate(str(?t),"áéíóú","aeiou") as ?noAccent2) .
} limit 100
To this moment, I've tried with XPath functions fn:contains, fn:compare, fn:translate, fn:starts-with, but none of them seem to be working.
Is there any other way (other than chaining replaces) to add collation into these functions or achieve the same goal?
The XPath functions you mention are not part of the SPARQL standard really, so as you found out, you can't rely on them being supported out of the box (though some vendors may provide them as an add-on).
However, GraphDB (which is based on RDF4J) allows you to create your own custom functions in SPARQL. It is a matter of writing a Java class that implements the org.eclipse.rdf4j.query.algebra.evaluation.function.Function interface, and registering it in the RDF4J engine by packaging it as a Java Service Provider Interface (SPI) implementation.
SPARQL and REGEX do not support efficiently transliterating character maps. If you want an efficient implementation you would need a custom RDF4J custom as described by Jeen.
If you want a quick and dirty solution use this code sample:
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
PREFIX spif: <http://spinrdf.org/spif#>
select * where {
BIND("Mariana" as ?t) .
BIND("Márénísótú" as ?t2) .
BIND (regex(str(?t),'^Maria') as ?noAccent1) .
BIND (spif:replaceAll(
spif:replaceAll(
spif:replaceAll(
spif:replaceAll(
spif:replaceAll(str(?t2),"á","a"),
"é","e")
,"í","i"),
"ó","o"),
"ú","u") as ?noAccent2) .
}
In following link
https://msdn.microsoft.com/en-us/library/ms175178.aspx
i found this cote "This prefix is then used in the query body instead of the namespace URI"
This makes me fell that if i do not specify prefix then i have to use uri in place of prefix. So please give me an example of using uri in place of prefix,
because in above link it is not given. only they given example of with prefix.
yours sincerley
This question is not a good question: Please read How to ask a good SQL question and How to create a MCVE
But I'll try to answer it...
A namespace is meant to separate identically named elements. Very often XML fragments are just glued together. It is very likely, that different elements with the same name get together. By using a namespace the combination of namespace and element name is used to identify the element. But it is not important, which alias is used to point to the namespace!
You must distinguish between the "namespace" and the given "alias".
The following example has the element <a> in two flavours. Check this out:
DECLARE #xml XML=
N'<root xmlns="DefaultNS" xmlns:dns="DerivedNS">
<a>This is default</a>
<dns:a>This is derived</dns:a>
</root>';
--no result
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'/root/a') AS A(a);
--wildcard-namespace: Everything
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'/*:root/*:a') AS A(a);
--Only default namespace declared: Only default element returned
WITH XMLNAMESPACES(DEFAULT 'DefaultNS')
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'/root/a') AS A(a);
--.nodes() calls for the derived namespace: Only derived element returned
WITH XMLNAMESPACES(DEFAULT 'DefaultNS'
,'DerivedNS' AS xyz) --Other prefix, doesn't matter
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'/root/xyz:a') AS A(a); --prefix "dns" would not work, the alias is now "xyz"
--Here I use random prefixes. I took "dns" as alias for the default!
WITH XMLNAMESPACES('DefaultNS' AS dns
,'DerivedNS' AS dns2) --random prefixes for the namespaces, took even dns for the wrong one!
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'/dns:root/dns:a') AS A(a); --prefix "dns" returns the default ns now! And you need the prefix at `root` too!
--With inline declaration
SELECT a.value(N'(text())[1]',N'nvarchar(max)')
FROM #xml.nodes(N'declare namespace x="DefaultNS"; /x:root/x:a') AS A(a); --The alias "x" is now bound to the default namespace!
I am trying to teach myself this weekend how to run API queries against a data source in this case data.gov. At first I thought I'd use a simple SQL variant, but it seems in this case I have to use SPARQL.
I've read through the documentation, downloaded Twinkle, and can't seem to quite get it to run. Here is an example of a query I'm running. I'm basically trying to find all gas stations that are null around Denver, CO.
PREFIX station: https://api.data.gov/nrel/alt-fuel-stations/v1/nearest.json?api_key=???location=Denver+CO
SELECT *
WHERE
{ ?x station:network ?network like "null"
}
Any help would be very much appreciated.
SPARQL is a graph pattern language for RDF triples. A query consists of a set of "basic graph patterns" described by triple patterns of the form <subject>, <predicate>, <object>. RDF defines the subject and predicate with URI's and the object is either a URI (object property) or literal (datatype or language-tagged property). Each triple pattern in a query must therefore have three entities.
Since we don't have any examples of your data, I'll provide a way to explore the data a bit. Let's assume your prefix is correctly defined, which I doubt - it will not be the REST API URL, but the URI of the entity itself. Then you can try the following:
PREFIX station: <http://api.data.gov/nrel...>
SELECT *
WHERE
{ ?s station:network ?network .
}
...setting the PREFIX to correctly represent the namespace for network. Then look at the binding for ?network and find out how they represent null. Let's say it is a string as you show. Then the query would look like:
PREFIX station: <http://api.data.gov/nrel...>
SELECT ?s
WHERE
{ ?s station:network "null" .
}
There is no like in SPARQL, but you could use a FILTER clause using regex or other string matching features of SPARQL.
And please, please, please google "SPARQL" and "RDF". There is lots of information about SPARQL, and the W3C's SPARQL 1.1 Query Language Recommendation is a comprehensive source with many good examples.
Is it possible to have a . (dot) in the qname of individuals or RDF resources in general?
Something like this?
SELECT ?tableName ?fieldName
WHERE { ?fieldName hrdata:relatedField hrdata:ps_ti0002.EMPLID. }
The dot in ps_ti0002.EMPLID is problematic.
your code is right and should work. It is possible to use dot in the individual's name.
I think you should check your data property (relatedField), maybe is not clarified right.
#Narges Kasaeizadeh
Unfortunately, I still can't comment - but I think your answer is wrong and a dot is not allowed in prefixed URIs/IRIs as you can try out using the validator suggested by #AndyS .
The . is allowed in SPARQL and the SPARQLer Query Validator demonstrates. However, there are a couple of suggestions to help you get this working. First is to have a space after the qname, i.e.:
WHERE { ?fieldName hrdata:relatedField hrdata:ps_ti0002.EMPLID . }
Another is to use the fully qualified URI. Suppose the namespace for hrdata is http://example.org/hrdata/, the the following query should work:
SELECT ?tableName ?fieldName
WHERE { ?fieldName hrdata:relatedField <http://example.org/hrdata/ps_ti0002.EMPLID> . }