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

能否在不使用XSLT、Java或Python的情况下动态命名XPath中的标识符?

问题:能否在不使用XSLT、Java或Python的情况下动态命名标识符?

输入数据结构

<persons>
  <person>
    <first>Joe</first>
    <last>Brown</last>
    <email>joebrown@example.com</email>
    <birthdate>2000-01-01</birthdate>
    <schools>
      <school>
        <ceeb>380880</ceeb>
      </school>
      ...
    </schools>
  </person>

  ...
</persons>

常规XML格式

<layout type="xml" node="/persons/person">
  <f s="first" id="First Name" />
  <f s="last" id="Last Name" />
  <f s="email" id="Email" />
  <f s="birthdate" id="Birthdate" />
  <f s="schools/school[1]/ceeb" id="School 1 Code"/>
  <f s="schools/school[2]/ceeb" id="School 2 Code" />
</layout>

需求与期望格式

需要为id属性实现动态命名,期望生成如下格式:

<layout type="xml" node="/persons/person">
  <f s="first" id="First Name" />
  <f s="last" id="Last Name" />
  <f s="email" id="Email" />
  <f s="birthdate" id="Birthdate" />
  <f s="schools/school[position() = 1]/ceeb" id="School position() Code"/>
  <f s="schools/school[position() = 2]/ceeb" id="School position() Code" />
</layout>

回答

这取决于你使用的XML处理工具或平台是否支持在属性值中直接解析执行XPath表达式(比如position()这类函数)。如果工具本身提供了属性值的动态表达式解析能力,那不用XSLT、Java或Python也能实现需求。

但要明确:标准XML本身是静态的,id属性里的position()只是普通字符串,不会自动计算出对应数值。只有当你的处理系统能识别并执行这个表达式时,才能实现动态命名的效果。

如果你的平台不支持这种内嵌表达式解析,那不用XSLT、Java或Python这类工具的话,几乎无法实现动态生成带位置编号的id属性——毕竟静态XML无法自行动态计算节点位置并替换属性值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 09:13:12