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

Node.js中使用XSLT3实现JSON转JSON的遍历方法求助

XSLT 3.0在Node.js下实现JSON到JSON字段映射方案

首先先修正一个前置问题:你提供的输入JSON存在语法错误,"remId": 140、"displayProduct": "ADT"两行末尾缺少逗号,执行转换前必须补全,否则JSON解析会直接失败。

核心问题说明

你现有代码不生效的核心原因:

  • match="*"是用来匹配XML元素节点的规则,XSLT 3.0处理JSON时,会将JSON解析为XDM规范的map、array、原子值结构,不是XML节点,所以这个模板根本不会被触发。
  • JSON转JSON场景不需要写节点匹配模板,直接以命名初始模板作为入口,构造输出map结构即可。

可直接运行的实现代码

1. 支持自定义字段映射的XSLT代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map"
  exclude-result-prefixes="#all">
  
  <xsl:output method="json" indent="yes"/>

  <!-- 全局字段映射表:key为输入JSON原始字段名,value为输出自定义标签名,修改映射直接改这里即可 -->
  <xsl:param name="fieldMapping" as="map(xs:string, xs:string)" select="
    map{
      'customerName': 'Name',
      'myCustomerId': 'Customer Account No'
    }
  "/>

  <!-- 支持外部传入输入JSON数据,Node.js调用时可直接把JSON对象作为参数传入 -->
  <xsl:param name="inputJson" as="map(*)"/>
  
  <!-- 转换入口,JSON转换固定用初始模板启动 -->
  <xsl:template name="xsl:initial-template">
    <xsl:variable name="customerDetail" select="$inputJson?customerDetail"/>
    <!-- 遍历映射表生成输出结构 -->
    <xsl:sequence select="
      map:merge(
        for $sourceField in map:keys($fieldMapping)
        return map{
          $fieldMapping($sourceField): $customerDetail($sourceField)
        }
      )
    "/>
  </xsl:template>
  
</xsl:stylesheet>

2. Node.js侧调用代码(以Saxon-JS为例,Node.js生态下最稳定的XSLT 3.0实现)

const SaxonJS = require('saxon-js');

// 输入JSON,记得补全之前提到的缺失逗号
const inputData = {
  "customerDetail": {
    "productId": 3455533,
    "customerName": "TEST REDFOL",
    "vendorCustId": "66209776",
    "accountId": "6609776",
    "myCustomerId": "7777376790",
    "customerStatus": "SCHEDULED TO REMOVE",
    "ctryCode": "CA",
    "parentService": 4,
    "remId": 140,
    "displayProduct": "ADT",
    "carrierNameLabel": "Carrier Name"
  }
};

// 执行转换
SaxonJS.transform({
  stylesheetLocation: './your-xslt.sef.json', // 用SaxonJS编译XSLT生成的sef文件路径
  initialTemplate: 'xsl:initial-template',
  stylesheetParams: {
    inputJson: inputData
  },
  destination: 'serialized'
}).then(res => {
  console.log(res.principalResult); // 输出就是你要的目标JSON
});

关键语法说明

  • JSON取值:XDM map类型用?操作符取属性,比如$inputJson?customerDetail就是取根节点下的customerDetail对象,$customerDetail?customerName就是取对应字段值。
  • 遍历逻辑:如果要处理JSON数组,直接用XPath的for $item in $array return ...语法即可,和普通循环逻辑一致,比如遍历customerDetail数组只需要把取值逻辑改成循环即可。
  • 简化写法:如果字段很少不需要可配置映射,直接硬编码输出结构即可,不需要写映射表遍历:
    <xsl:template name="xsl:initial-template">
      <xsl:param name="inputJson" as="map(*)"/>
      <xsl:variable name="cust" select="$inputJson?customerDetail"/>
      <xsl:sequence select="map{
        'Name': $cust?customerName,
        'Customer Account No': $cust?myCustomerId
      }"/>
    </xsl:template>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 22:40:50