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

如何用XSLT转换JSON并提取指定属性值,输出JSON格式

用XSLT提取JSON指定属性并输出JSON结果

下面针对给定的JSON数据,提供两种常见提取需求的XSLT实现方案,输出均为JSON格式。

场景1:提取"squadName"属性值

XSLT代码

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="json" indent="yes"/>
  
  <xsl:template match="/">
    <xsl:variable name="json-data" select="json-doc('input.json')"/>
    <xsl:map>
      <xsl:map-entry key="'extractedSquadName'" select="$json-data?squadName"/>
    </xsl:map>
  </xsl:template>
</xsl:stylesheet>

输出结果

{
  "extractedSquadName": "Super hero squad"
}

场景2:提取members数组第一个索引的元素

XSLT代码

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="json" indent="yes"/>
  
  <xsl:template match="/">
    <xsl:variable name="json-data" select="json-doc('input.json')"/>
    <xsl:map>
      <xsl:map-entry key="'firstMember'" select="$json-data?members[1]"/>
    </xsl:map>
  </xsl:template>
</xsl:stylesheet>

输出结果

{
  "firstMember": {
    "name": "Molecule Man",
    "age": 29,
    "secretIdentity": "Dan Jukes",
    "powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
  }
}

注:以上代码基于XSLT 3.0实现,需要支持XSLT 3.0的处理器(比如Saxon)才能运行,input.json为你的目标JSON文件路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:47:29