使用 XSLT 将 XML 转换为 JSON - 删除根元素和动态数据类型

如何解决使用 XSLT 将 XML 转换为 JSON - 删除根元素和动态数据类型

我需要帮助使用 XSLT 将 XML 转换为 JSON,还需要删除根元素并为 JSON 元素设置正确的数据类型。

我可以成功地从 XML 转换为 JSON 并删除根元素,但是,它没有反映正确的数据类型,认情况下输出是文本/字符串。 任何人都可以帮我解决这个问题吗? 一个重要的是基于参数,结构(是动态的)也可以改变,添加新列或删除列。

示例: 来源:

    {
   "denodo:view":{
      "name":"students","students":{
         "studentNumber":"JT60J8","studentName":"Adam","courses":2,"price":4700.810,"discount":956.20,"createDate":1291248000,"lastUpdateDate":1291248000
      }
   }
}

目标:

“学生”:{ "studentNumber":"JT60J8","学生姓名":"亚当",“课程”:2, “价格”:4700.810, “折扣”:956.20, “创建日期”:1291248000, “上次更新日期”:1291248000 }

使用的 XSLT:

  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="text" />
    <xsl:variable name="nl">
        <xsl:text>&#xa;</xsl:text>
    </xsl:variable>
    <xsl:variable name="tb">
        <xsl:text>&#x9;</xsl:text>
    </xsl:variable>
    <xsl:template match="/*">
        <!-- Open the root array -->
        <!--<xsl:text>[{</xsl:text>-->
    <xsl:text>{</xsl:text>
        <xsl:value-of select="$nl" />
        <!-- Process all the child nodes of the root -->
        <xsl:apply-templates select="*" mode="subitem" >
            <xsl:with-param name="indent" select="$tb" />
        </xsl:apply-templates>
        <!-- Close the root array -->
        <xsl:value-of select="$nl" />
        <!--<xsl:text>}]</xsl:text>-->
  <xsl:text>}</xsl:text>
    </xsl:template>
    <xsl:template match="*" mode="subitem" >
        <!-- child element at any level. The indent parameter allows for better layout of the output JSON -->
        <xsl:param name="indent"/>
        <!-- newindent is used as the indent for children of this node -->
        <xsl:variable name="newindent">
            <xsl:value-of select="$indent" />
            <xsl:value-of select="$tb"/>
        </xsl:variable>
        <!-- output the name of this node in quotes,ready for the content to follow -->
        <xsl:value-of select="$indent" />
        <xsl:text>"</xsl:text>
        <xsl:value-of select="name()" />
        <xsl:text>" :</xsl:text>
        <!-- check if this node has children,if not,simply output the text value,otherwise outoput an array -->
        <xsl:choose>
            <xsl:when test=" count( ./* ) = 0 ">
                <!-- This is a text value only -->
                <xsl:text> "</xsl:text>
                <!-- Make sure that any embedded quotes are escaped -->
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="." />
                    <xsl:with-param name="replace">"</xsl:with-param>
                    <xsl:with-param name="by">\"</xsl:with-param>
                </xsl:call-template>
                <xsl:text>"</xsl:text>
                <!-- check if we need a comma and a new line,not required if this is the last output value -->
                <xsl:if test=" position() != last() ">
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$nl" />
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <!-- This node has children,so we need to process them as an array -->
                <!-- Array opening -->
                <xsl:text>[</xsl:text>
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$newindent" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$nl" />
                <!-- Process all the elements in the array (recursive call to this template) -->
                <xsl:apply-templates select="*" mode="subitem" >
                    <xsl:with-param name="indent">
                        <xsl:value-of select="$newindent" />
                    </xsl:with-param>
                </xsl:apply-templates>
                <!-- Close the array -->
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$newindent" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$indent" />
                <xsl:text>]</xsl:text>
                <!-- If this is not the last node then we need a comma and line Feed -->
                <xsl:if test=" position() != last() ">
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$nl" />
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="string-replace-all">
        <!-- This code provided thanks to @codesling on stackoverflow -->
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text"
                        select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?