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

咨询:如何在DITA中复用文本并实现上下文变量替换?

解决方案建议

针对你在DITA中复用文本并根据不同单元替换变量的需求,以下几种可行方案:

方案一:Keyscope + Keyref 本地定义

这是最直接的方法,核心是让每个单元的topic文件维护自己的变量key,共享片段通过keyref调用,利用keyscope隔离不同单元的key定义:

  • 在unit_a.dita中,添加本地keyscope并定义单元名称key:
    <topic id="unit_a" xmlns="http://dita.oasis-open.org/architecture/2005/">
      <keyscope keys="unit_a_scope">
        <keydef keys="unit_name" text="Unit A"/>
      </keyscope>
      <!-- 引用共享片段 -->
      <p conref="unit_a_b_shared.dita#shared/config_step"/>
    </topic>
    
  • 在unit_b.dita中做同样配置,仅修改key的text值:
    <topic id="unit_b" xmlns="http://dita.oasis-open.org/architecture/2005/">
      <keyscope keys="unit_b_scope">
        <keydef keys="unit_name" text="Unit B"/>
      </keyscope>
      <p conref="unit_a_b_shared.dita#shared/config_step"/>
    </topic>
    
  • 在共享文件unit_a_b_shared.dita中,用keyref替换固定文本:
    <topic id="shared" xmlns="http://dita.oasis-open.org/architecture/2005/">
      <p id="config_step">When you configure <ph keyref="unit_name"/> to ...</p>
    </topic>
    
    原理:conref引用的内容会继承引用者的keyscope上下文,因此不同单元的topic会解析出对应的单元名称。

方案二:Conref Push 传递变量值

通过conref的推送功能,在引用共享片段时直接替换占位符:

  • 在共享文件unit_a_b_shared.dita中设置占位元素:
    <topic id="shared" xmlns="http://dita.oasis-open.org/architecture/2005/">
      <p id="config_step">When you configure <ph id="unit_placeholder"/> to ...</p>
    </topic>
    
  • 在unit_a.dita中引用并推送替换内容:
    <topic id="unit_a" xmlns="http://dita.oasis-open.org/architecture/2005/">
      <ph id="unit_a_name">Unit A</ph>
      <p conref="unit_a_b_shared.dita#shared/config_step">
        <ph conrefpush="#unit_a_name"/>
      </p>
    </topic>
    
  • unit_b.dita同理,仅修改推送的目标元素内容即可。

方案三:使用DITA 1.3 元素结合Keyscope

和方案一逻辑一致,但用<var>元素更贴合变量的语义:

  • 在单元topic的keyscope中定义变量key:
    <keydef keys="unit_var" text="Unit A"/>
    
  • 共享片段中使用:
    <p>When you configure <var keyref="unit_var"/> to ...</p>
    

注意事项

  • 确保你的DITA处理器支持keyscope和conref push(DITA-OT 3.0及以上版本、Oxygen XML等工具均支持)。
  • 避免在共享片段中使用绝对路径的keyref,保持key名称的通用性,让引用者的keyscope负责解析。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:55:35