You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MarkLogic插入TDE模板报无效模板错误如何解决

MarkLogic TDE模板创建报错修复方案

现有场景梳理

你的MarkLogic数据库patents目录下存有4份专利JSON文档,路径为:

  • /patents/airless_diving.json
  • /patents/smart_racket.json
  • /patents/tuning_ukulele.json
  • /patents/waterski_drone.json

单份文档结构示例:

{
  "patent": {
    "title": "Airless SCUBA diving", 
    "inventor": "Greg", 
    "description": "Diving pill that provides airless SCUBA diving for up to 1 hour"
  }
}

执行模板插入操作时抛出错误:

[javascript] TDE-INVALIDTEMPLATE: (err:FOER0000) tde.templateInsert('/templates/inventionsTemplate.json', inventionsTemplate); -- Invalid TDE template: TDE-INVALIDTEMPLATENODE: Invalid extraction template node: fn:doc("")/template/array-node("rows")/object-node()


1. 适配当前专利场景的正确TDE语法

你编写的模板存在3处不符合TDE规范的问题,直接导致节点提取失效报错:

  • context路径写法错误:JSON文档的XPath需从根节点算起,patent是根节点下的一级属性,必须写绝对路径/patent,不能直接写patent
  • directories路径格式错误:TDE要求目录匹配规则必须以/开头、以/结尾,你写的"patents"不符合规则,需改为"/patents/"
  • 列值val的相对路径错误:val的路径是相对于context定位的节点计算的,当context已经定位到/patent节点时,提取子属性直接写属性名即可,不需要加../向上跳转,否则会定位到根节点找不到对应字段

修正后的可直接运行的模板代码:

const tde = require('/MarkLogic/tde');

const inventionsTemplate = xdmp.toJSON(
{
  "template": {
    "context": "/patent",
    "directories": ["/patents/", "/another_patents/"],
    "rows": [
      {
        "viewName": "inventions",
        "columns": [
          {
            "name": "title",
            "scalarType": "string",
            "val": "title",
            "nullable": true
          },
          {
            "name": "inventor",
            "scalarType": "string",
            "val": "inventor",
            "nullable": true
          },
          {
            "name": "description",
            "scalarType": "string",
            "val": "description",
            "nullable": true
          }
        ]
      }
    ]
  }
}
);

// 校验通过后再插入
const validateRes = tde.validate(inventionsTemplate);
if (validateRes.valid) {
  tde.templateInsert('/templates/inventionsTemplate.json', inventionsTemplate);
} else {
  throw new Error(validateRes.error);
}

2. 插入TDE模板前的必要前置准备

你遗漏了3项必要的前置检查/配置:

  • 权限检查:执行tde.templateInsert操作的账号必须拥有tde-admin内置角色,普通的文档读写角色没有TDE模板的操作权限;插入的模板本身需要配置读取权限,确保后续查询视图的账号有权限访问模板
  • 存储位置检查:如果你的环境做了内容数据库和Schemas数据库的分离部署,TDE模板必须插入到内容数据库关联的Schemas数据库中,直接插入内容数据库的模板不会被TDE引擎加载生效
  • 模板合法性校验:正式插入前必须调用tde.validate()接口校验模板语法、路径的合法性,避免直接插入时报错中断,校验逻辑已经附在上面的示例代码中。

内容的提问来源于stack exchange,提问作者Nadia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 03:01:42