You need to enable JavaScript to run this app. XSLT3.0使用for-each提取JSON Map中关联数组值的实现问题
优惠活动
大模型
产品
解决方案
定价
更多

XSLT3.0使用for-each提取JSON Map中关联数组值的实现问题

解决方法

你只需要启用注释的内层xsl:for-each逻辑,适配json-to-xml()转换后的XML结构即可,修改后的对应模板代码如下:

<!-- Transform balance data -->
<xsl:template match="*[@key = 'balance']">
  <report:yearly-values>
    <xsl:for-each select="./*">
      <!-- 内层遍历JSON数组转换后的数值节点 -->
      <xsl:for-each select="./number">
        <xsl:element name="cells:{../@key}">{.}</xsl:element>
      </xsl:for-each>
    </xsl:for-each>
  </report:yearly-values>
</xsl:template>

逻辑说明

XSLT 3.0的json-to-xml()函数对JSON结构的转换规则如下:

  • JSON对象的键会被转换为XML节点的key属性
  • JSON数组的每个元素会被转换为对应键节点的子节点,数值类型的元素会被包裹在<number>标签中
  • 内层循环上下文为<number>节点时,通过../@key获取上层的键名(cash/bank),通过{.}直接输出当前数值即可得到预期结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 17:24:02