Using SPARQL's URI( ) fn w/PREFIX & suffix containing slashes? - uri

I want to use a PREFIX to simplify URI creation. Will welcome any advice that helps me build a mental model of what PREFIX is doing in a SPARQL query - it doesn't seem to a simple key/value replacement.
Here are some examples of what have tried.
working
This works as expected and does what I want, except for not using a PREFIX.
SELECT * WHERE {
BIND ( URI("http:://www.foo.com/bar/01/grik/234") as ?s ) # (a) works fine
?s a ?o .
# Here (a) works as expected. I'm binding ?s to a specific URI
# for testing because otherwise it runs too long to debug my query logic.
}
LIMIT 10
My failed PREFIX attempts
My actual prefix URI fragment is longer but this example shows the idea.
I want to put the first part of the above URI, http:://www.foo.com/bar/, in a PREFIX and use 01/grik/234 as a suffix.
Variations of this return nothing or error out on URI composition:
PREFIX foo: <http:://www.foo.com/bar/>
SELECT * WHERE {
# I'm just running run one of these BIND statements
# at a time; listing all of them here for easier visual comparison.
# BIND ( URI(foo:01/grik/234) as ?s ) # (b) Lexical error. Encountered "/" after "grik"
# BIND ( URI(foo:"01/grik/234") as ?s ) # (c) Encountered " <STRING_LITERAL2> "\01/grik/234"\""
# BIND ( URI(foo:URI("01/grik/234")) as ?s ) # (d) Encountered "/" after "01"
# BIND ( URI(foo:ENCODE_FOR_URI("01/grik/234")) as ?s ) # (e) Encountered "/" after "01"
# BIND( URI(foo:ENCODE_FOR_URI("01/grik/234")) as ?s ) # (f) WARN URI <http:://www.foo.com/bar/ENCODE_FOR_URI> has no registered function factory
?s a ?o .
}
LIMIT 10

You're trying to use a IRI in its prefixed name form. The W3C SPARQL recommendation contains the following section
4.1.1.1 Prefixed Names
The PREFIX keyword associates a prefix label with an IRI. A prefixed name is a prefix label and a local part, separated by a colon
":". A prefixed name is mapped to an IRI by concatenating the IRI
associated with the prefix and the local part. The prefix label or the
local part may be empty. Note that SPARQL local names allow leading
digits while XML local names do not. SPARQL local names also allow the
non-alphanumeric characters allowed in IRIs via backslash character
escapes (e.g. ns:id\=123). SPARQL local names have more syntactic
restrictions than CURIEs.
Given that / is a non-alphanumeric character, the most important part here is
SPARQL local names also allow the non-alphanumeric characters allowed
in IRIs via backslash character escapes (e.g. ns:id\=123).
Long story short, your query should be
PREFIX foo: <http:://www.foo.com/bar/>
SELECT * WHERE {
BIND ( URI(foo:01\/grik\/234) as ?s )
?s a ?o .
}
LIMIT 10

Related

Dynamic Path in XQUERY

This is my xml-file:
<?xml version="1.0" encoding="UTF-8"?>
<QQ:Envelope xmlns:QQ="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<RR:ABCInfo xmlns:RR="http://abc.test.de/abc/SOAP-Header/1.0">
<RR:Version>2.2.2.2</RR:Version>
<RR:BuildRevision>3333</RR:BuildRevision>
<RR:BuildTimestamp>2019-01-01T00:00:00.000+02:00</RR:BuildTimestamp>
<RR:Start>2019-01-01T10:10:10.101+02:00</RR:Start>
<RR:End>2019-01-01T11:11:11.111+02:00</RR:End>
<RR:Something>2.222 sek.</RR:Something>
<RR:Anything/>
</RR:ABCInfo>
<work:WorkContext xmlns:work="http://test.com/">1234567890abcdefghijklmnopqrstuvwxyz</work:WorkContext>
</SOAP-ENV:Header>
<QQ:Body>
<TT:testA xmlns:TT="http://abc.test.de/XYZ/2.0.1" xmlns:RR="http://abc.test.de/abc/abcdefgh/1.0">
<TT:testB>
<TT:testC>
<TT:testD>
<TT:testE id="1234567" quellID="09876543">
<TT:data>urn:de:abc:test:whatever</TT:data>
<TT:changeDate>2019-02-02T02:02:02.020+02:00</TT:changeDate>
<TT:part1 listURI="urn:de:abc:codeliste:555" listVersionID="V12">
<code>555_777</code>
<name>Fischers Fritze</name>
</TT:part1>
<TT:piece2>Frische Fische fischen</TT:piece2>
<TT:begin>
<TT:date>20191231</TT:date>
</TT:begin>
</TT:testE>
</TT:testD>
</TT:testC>
</TT:testB>
</TT:testA>
</QQ:Body>
</QQ:Envelope>
I have a XQuery, where I have to return XML. The first element in the returning XML is "result". The other elements in the returning XML should be dynamically created.
I get 2 sequences from outside, though I have made 2 fix Sequences in the following example to test it.
In Sequence No 1 I get the names for the other elements.
In Sequence No 2 I get the related path to the element names in Sequence 1.
I open the XML file an read a path (there might be several elements, though in my example is only one.
Then I want to process this result in a loop and return the dynamic elements.
If I access the path with a fix value (variable $c in the following code) I get the correct value, but then I must know the elements in Sequence 1 and the path in Sequence 2.
If I concatenate the path then I get the value from all elements.
This is my XQuery Code:
declare namespace TT="http://abc.test.de/XYZ/2.0.1";
declare namespace QQ="http://schemas.xmlsoap.org/soap/envelope/";
declare function local:getValue($path) as xs:string {
if (fn:exists($path)) then
(
data($path)
) else (
""
)
};
let $a := ('part1', 'piece2', 'beginDate')
let $b := ('TT:part1/name','TT:piece2', 'TT:begin/TT:date')
for $x in doc("Test.XML")/QQ:Envelope/QQ:Body/TT:testA/TT:testB/TT:testC/TT:testD/TT:testE
return <result>
{
for $item at $ind in $a
let $c := local:getValue($x/TT:part1/name)
let $d := local:getValue($x || concat("/", $b[$ind]))
return element { $item } {$c, " --- ", $d}
}
</result>
Is there a possibility to access the path dynamically?
Thank you in advance.
http://www.xqueryfunctions.com/xq/functx_dynamic-path.html could help - at least did it help ME ;)
The functx:dynamic-path function dynamically evaluates a simple path expression. The function only supports element names and attribute names preceded by #, separated by single slashes. The names can optionally be prefixed, but they must use the same prefix that is used in the input document. It does not support predicates, other axes, or other node kinds. Note that most processors have an extension function that evaluates path expressions dynamically in a much more complete way.

Xquery - Using sparql results to dynamically create XML. Dynamic Element Names

I am using MarkLogic 8.
I have a SPARQL statement as such.
let $results :=
sem:sparql(
"
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX slc: <http://www.smartlogic.com/2014/08/semaphore-core#>
select ?relation ?value
where
{
$input ?relation ?c .
OPTIONAL {
?c skosxl:prefLabel/skosxl:literalForm|skosxl:literalForm ?d .
}
BIND ( if (bound(?d), ?d, ?c) as ?value )
}
", $bindings
)
This gives me back results that are a list of (relation, value) pairs.
I am trying to turn this response into an XML document that will be stored statically.
I've tried a variety of different approaches.
Attempt 1
let $doc := <test>{
for $item in $results
return element {map:get($item, 'relation')} {map:get($item, 'value')}
}</test>
return $doc
Error :
XDMP-ELEMNAME: (err:XPTY0004) for $item in $results -- Cannot use
sem:iri("http://www.w3.org/2008/05/skos-xl#altLabel") as an element
name
I tried casting the item in question, to a string using fn:string but that leads to a
[1.0-ml] XDMP-QNAMELEXFORM: for $item in $results -- Invalid lexical
form for QName
How can i declare a dynamic element name in XQuery during XML Building?
What is causing this error in the first place? I have been messing with syntax to try and figure it out, what am I unaware of that is causing this issue?
Thank you for reading.
Casting as string should be enough.
However, your example has foreward slashes which I believe are invalid.
Second, your example would be making an element defined as being in the html namespace - or whatever you defined the prefix html to be.
Also, the first char after the colon is not an alphanumeric characters which is required.
In my opinion, the name you are trying to use for an element name is the issue - not the actual approach.

KSH script checks for alphanumeric in case statement

Below is a simplified model of what I'm trying to achieve:
#!bin/ksh
string=AUS00
case $string in
[[:alnum:]] ) echo "alphanumeric" ;;
*) echo "nope" ;;
esac
I'm unable to validate alphanumeric code.
Constraints:
The validation need to happen inside the case statement
alnum function is not supported
Positive check only. Can't check for the absence of alphanumeric.
Thank you very much
The pattern [[:alnum:]] will match a single alphanumeric character. Your string is longer than one character, so it won't match.
If you want to check that your string contains an alnum character, you want *[[:alnum:]]*
If you want to check that your string only contains alnum characters, I'd flip the check to see if the string contains a non-alnum character:
for string in alnumOnly 'not all alnum'; do
case "$string" in
*[^[:alnum:]]*) echo "$string -> nope" ;;
*) echo "$string -> alphanumeric" ;;
esac
done
alnumOnly -> alphanumeric
not all alnum -> nope
I realized that ksh (even ksh88) implements what bash describes as "extended patterns":
A pattern-list is a list of one or more patterns separated
from each other with a |. Composite patterns can be formed
with one or more of the following:
?(pattern-list)
Optionally matches any one of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given
patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
#(pattern-list)
Matches exactly one of the given patterns.
!(pattern-list)
Matches anything, except one of the given patterns.
So we can do:
case "$string" in
+([[:alnum:]]) ) echo "$string -> alphanumeric" ;;
* ) echo "string -> nope" ;;
esac

Oracle named parameters

How can I use keywords with Oracle named parameters syntax ? The following gives me 'ORA-00936: missing expression' because of the 'number'-argument:
select b.g3e_fid
, a.g3e_fid
, sdo_nn_distance( 1)
from acn a, b$gc_fitface_s b
where mdsys.sdo_nn ( geometry1 => a.g3e_geometry, geometry2 => b.g3e_geometry, param => 'sdo_num_res=1', number=>1) = 'TRUE' and b.g3e_fid = 57798799;
If I run it without named parameters it is fine.
thanks, Steef
Although you can get around the reserved word issue in your call by enclosing the name in double quotes as #AvrajitRoy suggested, i.e. ... "NUMBER"=>1) = 'TRUE'..., you aren't actually achieving much. Oracle is letting you refer to the parameters by name but it isn't doing anything with that information.
MDSYS.SDO_NN is a spatial operator, not a direct call to a function. There is a function backing it up - you can see from the schema scripts for MDSYS that it's actually calling prtv_idx.nn - but the names of the formal parameters of that function are not relevant. With some digging you can see those are actually called geom, geom2, mask etc., and there isn't one called number (and you can't have a formal parameter called number, even quoting it, as far as I can tell).
The formal parameters to the operator are not named, and are effectively passed through positionally. You can't skip an argument by naming the others, as you can with a function/procedure with arguments that have default values.
So that means you can call the parameters anything you want in your call; changing the names of the first three parameters in your call to something random won't stop it working.
It also means naming them in the call is a bit pointless, but if you're just trying to document the call then you can use some other meaningful name rather than 'number' if you don't want to quote it.
Hello as mentined in you question . There are two ways in which u can eliminate this RESERVED keyword ISSUE.
1) Use "" to use any RESERVED key word for calling. But remember this is not a good coding practice.
Eg >
SELECT b.g3e_fid ,
a.g3e_fid ,
sdo_nn_distance( 1)
FROM acn a,
b$gc_fitface_s b
WHERE mdsys.sdo_nn
( geometry1 => a.g3e_geometry,
geometry2 => b.g3e_geometry,
"param" => 'sdo_num_res=1',
"NUMBER"=>1) = 'TRUE'
AND b.g3e_fid = 57798799;
2) Secondly you can just call the function without using "=>" as shown below
Eg >
SELECT b.g3e_fid ,
a.g3e_fid ,
sdo_nn_distance( 1)
FROM acn a,
b$gc_fitface_s b
WHERE mdsys.sdo_nn
( a.g3e_geometry,
b.g3e_geometry,
'sdo_num_res=1',
1) = 'TRUE'
AND b.g3e_fid = 57798799;

Need URI list in marklogic database using Xquery

After executing the below xquery, resulted in whole content from XML but my objective is to get the list of URI.
let $i := cts:search(//root,
cts:element-value-query(
xs:QName("no"),
"123"))
return ($i)
If all you want is the URI, use cts:uris(). The 3rd parameter lets you define a query that will filter the URIs list.
So with your example this would work:
cts:uris(
(),
(),
cts:element-value-query(
xs:QName("no"),
"123")
)

Resources