While generating triples with Template Driven Extraction (TDE), you want to reuse a namespace while generating triples.
Applies to MarkLogic versions 9+
Use template variables to hold the prefix. By doing this, you don’t need to repeat the namespace itself, just concatenate the variable name with the rest of the IRI.
var tde = require("/MarkLogic/tde.xqy"); var myTriplesTemplate = xdmp.toJSON({ "template": { "context": "/match", "collections": [ "source1", "source2" ], "vars": [ { "name": "prefix-ex", "val": "'https://example.org#'" }, "triples": [ { "subject": { "val": "sem:iri($prefix-ex || 'sample-subject')" }, "predicate": { "val": "sem:iri($prefix-ex || 'sample-predicate')" }, "object": { "val": "sem:iri($prefix-ex || 'sample-object')" } }, ] } });
import module namespace tde = "https://marklogic.com/xdmp/tde" at "/MarkLogic/tde.xqy"; let $my-triples-template := <template xmlns="https://marklogic.com/xdmp/tde"> <context>/match</context> <collections> <collection>source1</collection> <collection>source2</collection> </collections> <vars> <var> <name>prefix-ex</name> <val>"https://example.org#"</val> </var> </vars> <triples> <triple> <subject> <val>sem:iri($prefix-ex || "sample-subject")</val> </subject> <predicate> <val>sem:iri($prefix-ex || "sample-predicate")</val> </predicate> <object> <val>sem:iri($prefix-ex || "sample-object")</val> </object> </triple> </triples> </template>
Templates allow the construction of variables, which can then be used in populating rows or triples. Notice that the variable names can use hyphenated names, even if you’re working with JavaScript. That’s because of the context in which they will be used. The “val” portion of a triple specification is evaluated as XQuery code. Hence, even in a JSON template, we have "subject": { "val": "sem:iri($prefix-ex || 'sample-subject')" }
.
Using variables in your templates reduces the amount of repeated code, thus decreasing the chances of a mistake creeping in.
By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.