Search code examples
marklogicmarklogic-10

How to get subject value from cts:triples output


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.


Solution

  • 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.