如何使用XSLT将JSON转换为另一个JSON

wlwcrazw  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(115)

我必须将输入作为JSON,然后使用XSLT将其转换为另一个JSON。
输入JSON:

{
  "members": [
    {
      "name": "Molecule Man1",
      "age": 29,
      "secretIdentity": "Dan Jukes"
    },
    {
      "name": "Molecule Man2",
      "age": 30,
      "secretIdentity": "Martin"
    }
  ]
}

我想将上面的JSON转换为如下所示:

{
   "resourceType": "Test",
   "type": {
      "name": "Member"
   },
   "displayName": "Molecule Man",
   "attributes": {
      "test": [
         {
            "value": "29"
         }
      ],
      "testing": [
         {
            "value": "Dan Jukes"
         }
      ]
   }
}

我尝试了以下代码,但无法获得所需的输出JSON。我收到错误SERE 0023 JSON序列化:无法处理长度为2的序列,并且还获得错误saxon:数组未被识别为****Saxon指令。Saxon扩展要求Saxon-PE或更高版本,同时尝试实现saxon::array.
我使用了Saxon HE(12.0)依赖项。

Java代码

package com.practice;

import net.sf.saxon.s9api.*;
import javax.xml.transform.stream.StreamSource;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStreamReader;

import java.nio.charset.StandardCharsets;

public class JsonToXsltToJson {
    public static void main(String[] args) throws FileNotFoundException, SaxonApiException {
        Processor processor = new Processor(false);
        TransformJson(processor);
    }

    static void TransformJson(Processor processor) throws 
 FileNotFoundException, SaxonApiException {
       
        JsonBuilder jsonBuilder = processor.newJsonBuilder();
        XdmValue input = jsonBuilder.parseJson(new InputStreamReader(new FileInputStream("input.json"), StandardCharsets.UTF_8));
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(new File("jsonTransform.xsl")));
        Xslt30Transformer xslt30Transformer = xsltExecutable.load30();
        xslt30Transformer.applyTemplates(input, xslt30Transformer.newSerializer(System.out));

    }
}

sample.xsl

<?xml version="1.0" encoding="utf-8"?>
<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"
                xmlns:mf="http://example.com/mf" 
                exclude-result-prefixes="#all">

    <xsl:param name="nodes" as="map(*)*"/>

    <xsl:template name="init">
        <xsl:apply-templates select="($nodes)"/>
    </xsl:template>

    <xsl:template  match=".">
        <xsl:map>
            <xsl:map-entry key="'resourceType'" select="'Test'"/>
            <xsl:map-entry key="'type'">
            <xsl:map-entry key="'name'" select="'Member'"/>
            </xsl:map-entry>
            <xsl:map-entry key="'displayName'" select= "?members?1?name"/>
            <xsl:map-entry key="'attributes'">
            <xsl:map-entry key="'age'">
            <xsl:map-entry key="'value'" select="?members?1?age"/>
            </xsl:map-entry>
                <xsl:map-entry key="'secretIdentity'">
                    <xsl:map-entry key="'value'" select="?members?1?secretIdentity"/>
                </xsl:map-entry>
            </xsl:map-entry>
        </xsl:map>
    </xsl:template>
    <xsl:output method="json" indent="yes"/>
</xsl:stylesheet>
nkoocmlb

nkoocmlb1#

您还可以在XPath中完成所有这些操作:

<xsl:sequence select="
  map{'resourceType':'Test',
      'type':map{'name':'Member'},
      'displayName': ?members?1?name,
      'attributes': map{
          'test': [?members?1?age],
          'testing': [?members?1?secretIdentity]}
     }"/>
tjvv9vkg

tjvv9vkg2#

我认为您需要一个嵌套Map:

<xsl:map>
        <xsl:map-entry key="'resourceType'" select="'Test'"/>
        <xsl:map-entry key="'type'">
          <xsl:map-entry key="'name'" select="'Member'"/>
        </xsl:map-entry>
        <xsl:map-entry key="'displayName'" select= "?members?1?name"/>
        <xsl:map-entry key="'attributes'">
          <xsl:map>
            <xsl:map-entry key="'test'" select="[?members?1?age]"/>
            <xsl:map-entry key="'testing'" select="[?members?1?secretIdentity]"/>
          </xsl:map>
        </xsl:map-entry>
    </xsl:map>

以及

<xsl:map-entry key="'test'" select="[?members?1?age]"/>
        <xsl:map-entry key="'testing'" select="[?members?1?secretIdentity]"/>

你可能

<xsl:map-entry key="'test'" select="[map { 'value' : ?members?1?age }]"/>
        <xsl:map-entry key="'testing'" select="[map { 'value' : ?members?1?secretIdentity }]"/>

相关问题