从cts:triples输出中获取主题值

huangapple go评论62阅读模式
英文:

How to get subject value from cts:triples output

问题

以下是您要求的翻译结果:

查询如下以获取来自“testCollection”的三元组列表,其中谓词为“testName”。

我可以在qconsole中看到以下结果。

我的要求是从结果中获取“主语/宾语”值。

例如,如果我想获取“主语”的值,那么输出应该如下:

a
b
c
d
e

而对于“宾语”,输出应该如下:

aa
bb
cc
dd
ee

任何帮助都会感激不尽。

英文:

I am executing below query to get the list of triples from "testCollection" where predicate is "testName".

cts:triples((), sem:iri("testName"), (), (), (), cts:and-query(cts:collection-query("testCollection"))).

I can see the result in qconsole as below.

<a> <testName> <aa> .
<b> <testName> <bb> .
<c> <testName> <cc> .
<d> <testName> <dd> .
<e> <testName> <ee> .

My requirement is to get the subject / object values from the result.

for example, let's say if I want to get the values of subject, then output should be as below,

a
b
c
d
e

and for object output should be as below,

aa
bb
cc
dd
ee

Any help is appreciated.

答案1

得分: 2

以下是您要求的翻译部分:

对于您的示例,使用cts函数,我将使用与之相关的项目:sem:triple-subject()sem:triple-object()

请记住,IRI和字符串不同。您的数据具有IRI,而您期望的结果是字符串。我提供一个匹配您所需的示例:

xquery version "1.0-ml";

(: Sample Output based on docs - sem triples
   Also, your data suggests that all items are IRIs :)
let $triples := (
  sem:triple(sem:iri("a"), sem:iri("testName"), sem:iri("aa")),
  sem:triple(sem:iri("b"), sem:iri("testName"), sem:iri("bb")),
  sem:triple(sem:iri("c"), sem:iri("testName"), sem:iri("cc"))
)

return for $triple in $triples
 return fn:string-join((xs:string(sem:triple-subject($triple)), xs:string(sem:triple-object($triple))), ",")

在这种情况下,我将其强制转换为xs:string()以匹配您期望的输出,并通过逗号连接主题和对象以进行显示。

a,aa
b,bb
c,cc

但是关于完整的IRI:

sem:triple(sem:iri("c"), sem:iri("testName"), sem:iri("http://www.example.com#cc"))

其结果将是:

c, http://www.example.com#cc

我不会进一步讨论这个问题 - 只是想指出,您应该了解IRI背后实际的内容,因为您的示例是简化的。

最后,如果我不是在cts空间中工作,我可能会编写更具体的SPARQL查询,以仅返回主题和对象,并使用xdmp:sparql()op:from-sparql()

英文:

For Your example, using cts functions, I would use the items related to that: sem:triple-subject() and sem:triple-object()

Please keep in mind that IRIs and Strings are not the same.. Your data has IRIs and your expected result is string. I present a sample that matches what You state that You want:

    xquery version "1.0-ml";

    (:

      Sample Output based on docs  - sem triples
      Also, your data suggests that all items are IRIs
    :)
    let $triples := (
      sem:triple(sem:iri("a"), sem:iri("testName"), sem:iri("aa")),
      sem:triple(sem:iri("b"), sem:iri("testName"), sem:iri("bb")),
      sem:triple(sem:iri("c"), sem:iri("testName"), sem:iri("cc"))
    )

    return for $triple in $triples
     return fn:string-join((xs:string(sem:triple-subject($triple)), xs:string(sem:triple-object($triple))), ",")

In this case, I cast as xs:string() to match your expected output and joined the subject and object by a comma for display purposes.

a,aa
b,bb
c,cc

But what about full IRIs:
sem:triple(sem:iri("c"), sem:iri("testName"), sem:iri("http://www.example.com#cc"))

The result of that result would be:
c, http://www.example.com#cc

I'll not take that further - just wanted to point out that you should be aware of what is actually behind an IRI since your sample was simplified.

Lastly, I would probably write a more specific SPARQL query to return just the subject and object and use xdmp:sparql() or op:from-sparql() if I were not in the cts space for my work already.

答案2

得分: 2

你可以使用sem:triple-object()sem:triple-subject()函数分别返回对象或主题。

for $triple in cts:triples((), sem:iri("testName"), (), (), (), cts:collection-query("testCollection"))
return sem:triple-object($triple)
英文:

You can use the sem:triple-object() and sem:triple-subject() functions to return just the object or the subject.

for $triple in cts:triples((), sem:iri("testName"), (), (), (), cts:collection-query("testCollection"))
return sem:triple-object($triple)

huangapple
  • 本文由 发表于 2023年7月6日 21:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定