0
votes

Carried over from this thread where Template Driven Extraction(TDE) was mentioned as a way to retrieve values from a certain element.

Currently, I am attempting to create multiple TDE templates at once for a certain path. The code I used to to accomplish this is written below

xquery version "1.0-ml"; 
import module namespace tde = "http://marklogic.com/xdmp/tde" 
  at "/MarkLogic/tde.xqy";

let $t1 :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/question</context>
    <vars>
      <var>
        <name>EX</name>
        <val>""</val>
      </var>
    </vars>
  <triples>
    <triple>
      <subject>
        <val>sem:iri( $EX || name)</val>
      </subject>
      <predicate>
        <val>sem:iri("/has_tags")</val>
      </predicate>
      <object>
        <val>xs:dateTime( $EX || tags)</val>
      </object>
    </triple>
  </triples>
  </template>

let $t2 :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/question</context>
    <vars>
      <var>
        <name>EX</name>
        <val>""</val>
      </var>
    </vars>
  <triples>
    <triple>
      <subject>
        <val>sem:iri( $EX || name)</val>
      </subject>
      <predicate>
        <val>sem:iri("/date_posted")</val>
      </predicate>
      <object>
        <val>xs:dateTime( $EX || date_posted)</val>
      </object>
    </triple>
  </triples>
  </template>

return(
tde:template-insert("question_has_tags.xml",$t1, (), "http://marklogic.com/xdmp/tde"),
tde:template-insert("question_date_posted.xml",$t2, (), "http://marklogic.com/xdmp/tde")
)

A sample document

<?xml  version="1.0" encoding="UTF-8"?>
<question>
<tags>uifontweighttrait,spark-thriftserver,alarmmanager,recursive-type,string-iteration,security-trimming,google-earth</tags>
<date_posted>1978-06-23T22:22:43</date_posted>
<name>/question_on_forum</name>
</question>

The template was tested using

tde:node-data-extract(fn:doc("/question"));

Which resulted in the error

[1.0-ml] TDE-EVALFAILED: tde:node-data-extract(fn:doc("/question/780989102826685")) -- Eval for Object='xs:dateTime( $EX || tags)' returns TDE-BADVALEXPRESSION: Invalid val expression: XDMP-CAST: (err:FORG0001) Invalid cast: "spotify,databags,laravel-horizon" cast as xs:dateTime

This error was solved the moment I removed one of the template

===========UPDATE===========

The codes below were used in an attempt to create multiple triples per template. However, it also results in the same error

xquery version "1.0-ml"; 
import module namespace tde = "http://marklogic.com/xdmp/tde" 
  at "/MarkLogic/tde.xqy";

let $t2 :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/question</context>
  <triples>
    <triple>
      <subject>
        <val>sem:iri( uri)</val>
      </subject>
      <predicate>
        <val>sem:iri("/date_posted")</val>
      </predicate>
      <object>
        <val>xs:dateTime(date_posted)</val>
      </object>
    </triple>
  </triples>
  <triples>
    <triple>
      <subject>
        <val>sem:iri(uri)</val>
      </subject>
      <predicate>
        <val>sem:iri("/has_tags")</val>
      </predicate>
      <object>
        <val>tags</val>
      </object>
    </triple>
  </triples>
  </template>
return(
tde:template-insert("question_date_posted.xml",$t2, (), "http://marklogic.com/xdmp/tde")
)

Node Extract for testing (This is where the error will occur)

xquery version "1.0-ml";

tde:node-data-extract(fn:doc("/question"));

Example Document Used

<?xml  version="1.0" encoding="UTF-8"?>
<question>
<tags>jsondate,jmsserializerbundle,omnikey,eula,template-deduction,pyclipper,asyncpg,naming,kubeconfig,timer-trigger</tags>
<date_posted>1986-02-21T22:27:50</date_posted>
<uri>/question</uri>
</question>
1
This won't work: xs:dateTime( $EX || tags), I think you just want tags there. And this won't work either: xs:dateTime( $EX || date_posted), it would have to be $EX || xs:dateTime(date_posted), but I expect you want to use xs:dateTime(date_posted).. - grtjn
By the way, particular reason why you are using separate templates? You can create multiple triples at once, and if you need conditional triples, use sub-templates with sub-context on the optional element.. - grtjn
Sorry I did this post while I was away from the actual codes, so I copied and pasted some portion without managing to edit everything properly. In any case, I have thought of creating multiple triples at once in a single template but it does not work as expected. I have edited the post to show what i attempted. @grtjn - WhiteSolstice
Your update shows partial code, maybe the error is of the other code? The double <triples> element looks a bit overkill, but doesn't seem to throw a validate error. - grtjn
Yes it actually goes through when adding to the schema and validating it shows "true" but the error is thrown upon execution of node-data-extract. Ill update with the neccessary code and data used so you maybe able to replicate it. @grtjn - WhiteSolstice

1 Answers

0
votes

Ok, I have found out the issue. It was a minor mistake on my part. The error is pointed out by a comment.

let $t2 :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/question</context>
  <triples>
    <triple>
      <subject>
        <val>sem:iri( uri)</val>
      </subject>
      <predicate>
        <val>sem:iri("/date_posted")</val>
      </predicate>
      <object>
        <val>xs:dateTime(date_posted)</val>
      </object>
    </triple>
  </triples>
  <triples>
    <triple>
      <subject>
        <val>sem:iri(uri)</val>
      </subject>
      <predicate>
        <val>sem:iri("/has_tags")</val>
      </predicate>
      <object>
        <val>xs:string(tags)</val>     #needed to determine the variable type of tags object
      </object>
    </triple>
  </triples>
  </template>