使用 XSLT 3.0 函数将 JSON 转换为 XML [英] Convert JSON to XML using XSLT 3.0 functions

查看:44
本文介绍了使用 XSLT 3.0 函数将 JSON 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手,我有一个简单的 JSON,我想使用 XSLT 3.0 将其转换为 XML.我按照 w3c 的建议尝试了 parsejson 和 jsontoxml 函数,但生成的输出不是所需的格式.为此,我正在使用 Saxonica 处理器.是否可以编写一个单独的 xslt,它可以在任何给定的特定结构的 json 中使用,而不管键名如何.否则我如何使用 xslt 功能将 json 转换为所需的 xml

I am new to XSLT, I have a simple JSON which I want to transform to an XML using XSLT 3.0. I tried parsejson and jsontoxml functions as per w3c recommendations, but produced output is not in desired format. I am using Saxonica processor for the purpose. Is it possible to write a single xslt which can be used in any given json of a particular structure irrespective of key names. Otherwise how can I approach to convert a json to desired xml using xslt features

我尝试了答案中的示例:[XSLT 3.0 中的 JSON 到 XML 转换,但对我不起作用

I tried the example in the answer: [JSON to XML transformation in XSLT 3.0, but is not working for me

输入 JSON

<data>{

      "Assets": [
  { "Asset": {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }},
      { "Asset": {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }}]

}
</data>

预期产出

<data>
   <Assets>
      <Asset>
         <Close>25.87</Close>
         <High>25.15</High>
         <Low>25.11</Low>
         <Open>25.15</Open>
      </Asset>
      <Asset>
         <Close>25.87</Close>
         <High>25.15</High>
         <Low>25.11</Low>
         <Open>25.15</Open>
      </Asset>
   </Assets>
</data>

我使用 xslt 获得的实际输出

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <array key="Assets">
      <map>
         <map key="Asset">
            <string key="Open">25.15</string>
            <string key="High">25.15</string>
            <string key="Low">25.11</string>
            <string key="Close">25.87</string>
         </map>
      </map>
      <map>
         <map key="Asset">
            <string key="Open">25.15</string>
            <string key="High">25.15</string>
            <string key="Low">25.11</string>
            <string key="Close">25.87</string>
         </map>
      </map>
   </array>
</map>

我的 XSLT 函数

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs math" version="3.0">
   <xsl:output indent="yes" />
   <xsl:template match="data">
      <xsl:copy-of select="json-to-xml(.)" />
   </xsl:template>
</xsl:stylesheet>

下面是我用撒克逊语进行转换的示例代码

        var xslt = new FileInfo("myxslt.xslt");
        var input = new FileInfo("inputxml.xml");

        // Compile stylesheet
        var processor = new Processor(true);
        var compiler = processor.NewXsltCompiler();
        var executable = compiler.Compile(new Uri(xslt.FullName));
        Serializer serializer = processor.NewSerializer();
        serializer.SetOutputFile(txtOutput.Text);

        // Do transformation to a destination
        var destination = new XdmDestination();
        using (var inputStream = input.OpenRead())
        {
            var transformer = executable.Load();
            transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
            transformer.Run(serializer);
        }

推荐答案

对于给定的输入用

<xsl:template match="data">
    <xsl:copy>
        <xsl:apply-templates select="json-to-xml(.)/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[@key]" xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
    <xsl:element name="{@key}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

但是,使用带有空格或冒号或 XML 元素名称中不允许的其他字符的 JSON 属性名称会很容易破坏这种方法.

however using JSON property names with spaces or colons or other characters not allowed in XML element names would break that approach easily.

这篇关于使用 XSLT 3.0 函数将 JSON 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆