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

使用XSLT转换含套装的书籍JSON为XML仅提取标题,求排查问题

解决JSON转XML时仅提取书籍标题的问题

你当前的XSLT只处理了title字段,未对Author、Language以及套装(coffret)内的子书籍做解析,导致其他信息丢失。以下是修正方案:

假设的输入XML(嵌入JSON)

<json>
{
  "books": [
    {
      "title": "单本测试书",
      "Author": "张三",
      "Language": "中文"
    },
    {
      "title": "经典文学套装",
      "coffret": [
        {
          "title": "红楼梦",
          "Author": "曹雪芹",
          "Language": "中文"
        },
        {
          "title": "西游记",
          "Author": "吴承恩",
          "Language": "中文"
        }
      ]
    }
  ]
}
</json>

修正后的XSLT代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- 顶层解析入口 -->
  <xsl:template match="/">
    <books>
      <xsl:call-template name="parse-book-array">
        <xsl:with-param name="json-content" select="substring-before(substring-after(json/text(), '['), ']')"/>
      </xsl:call-template>
    </books>
  </xsl:template>

  <!-- 遍历书籍数组元素 -->
  <xsl:template name="parse-book-array">
    <xsl:param name="json-content"/>
    <!-- 提取单个书籍的JSON片段 -->
    <xsl:variable name="single-book-json" select="substring-before(concat($json-content, '}'), '}')"/>
    <xsl:if test="$single-book-json != ''">
      <book>
        <!-- 提取title字段 -->
        <title>
          <xsl:value-of select="substring-before(substring-after($single-book-json, '&quot;title&quot;:&quot;'), '&quot;')"/>
        </title>
        <!-- 提取Author字段 -->
        <Author>
          <xsl:value-of select="substring-before(substring-after($single-book-json, '&quot;Author&quot;:&quot;'), '&quot;')"/>
        </Author>
        <!-- 提取Language字段 -->
        <Language>
          <xsl:value-of select="substring-before(substring-after($single-book-json, '&quot;Language&quot;:&quot;'), '&quot;')"/>
        </Language>
        <!-- 处理套装coffret的子书籍数组 -->
        <xsl:if test="contains($single-book-json, '&quot;coffret&quot;:')">
          <coffret>
            <xsl:call-template name="parse-book-array">
              <xsl:with-param name="json-content" select="substring-before(substring-after($single-book-json, '&quot;coffret&quot;:[') , ']')"/>
            </xsl:call-template>
          </coffret>
        </xsl:if>
      </book>
      <!-- 递归处理剩余书籍 -->
      <xsl:call-template name="parse-book-array">
        <xsl:with-param name="json-content" select="substring-after($json-content, concat($single-book-json, '},'))"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

转换命令行(以Saxon为例)

java -jar saxon9he.jar -s:your-input.xml -xsl:your-transform.xsl -o:output.xml

输出XML结果

<books>
   <book>
      <title>单本测试书</title>
      <Author>张三</Author>
      <Language>中文</Language>
   </book>
   <book>
      <title>经典文学套装</title>
      <Author/>
      <Language/>
      <coffret>
         <book>
            <title>红楼梦</title>
            <Author>曹雪芹</Author>
            <Language>中文</Language>
         </book>
         <book>
            <title>西游记</title>
            <Author>吴承恩</Author>
            <Language>中文</Language>
         </book>
      </coffret>
   </book>
</books>

关键修改说明

  • 新增Author、Language字段的提取逻辑,复用title的字符串截取方式
  • 增加coffret字段的判断逻辑,递归调用数组解析模板处理套装内的子书籍
  • 优化模板命名,让逻辑更清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:54:49