使用 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.JdbcOdbc...