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

在MarkLogic的TDE中如何保留position()函数的位置信息

TDE中获取XML 节点位置序号的解决方案

问题场景

给定示例XML:

<root>
<step>position 1 </step>
<step>position 2 </step>
<step>position 3</step>
</root>

这些<step>节点的顺序直接关联权重,且节点数量不固定。常规XPath里用position()能获取节点位置,但在TDE中把上下文设为<step>生成行时,position()始终返回1,拿不到正确的节点序号。

解决思路与示例配置

不要直接将TDE上下文设为<step>,而是把上下文定位到父节点<root>,再通过<foreach>遍历所有<step>节点生成行,此时position()就能正常计算每个节点在序列中的位置:

<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/root</context>
  <rows>
    <row>
      <logical-step>
        <foreach>/root/step</foreach>
        <columns>
          <column>
            <name>step_position</name>
            <scalar-type>int</scalar-type>
            <val>position()</val>
          </column>
          <column>
            <name>step_content</name>
            <scalar-type>string</scalar-type>
            <val>text()</val>
          </column>
        </columns>
      </logical-step>
    </row>
  </rows>
</template>

核心逻辑:将TDE上下文设为包含所有<step>的父节点后,position()的计算上下文变为整个<step>节点集合,而非单个<step>节点,因此能正确返回每个步骤在序列中的位置序号(1、2、3...)。

内容的提问来源于stack exchange,提问作者Marc Messéant

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 09:45:35