Problem

While generating triples with Template Driven Extraction (TDE), you want to reuse a namespace while generating triples.

Solution

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>

Discussion

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.

Learn More

TDE Documentation

Explore the documentation on how to get started with Template Driven Extraction in the Application Developer’s Guide.

TDE Technical Resources

Explore all the resources related to Template Driven Extraction (TDE), and learn more about how it can be used in MarkLogic.

MarkLogic Data Integration

Use your development skills to integrate data from silos to create an operational data hub in this hands-on course.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.