简体   繁体   中英

Is there any way to remove root nodes after converting XML to JSON using XSLT transformation

I have a requirement, where I have to send data from SAP to API. For this I need to convert XML to JSON format.I am new to XSLT. I have tried converting XML to JSON using XSLT Transformation. After converting, output should not contain Root nodes. And also is there a way of converting output JSON to x-www-form-urlencoded JSON. Input Data:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_Sales xmlns:ns0="urn:SD:Sales">
   <RequestData>
      <DODate>2020-02-10</DODate>
      <DONumber>1900200009</DONumber>
      <OrderNumber>3900002600</OrderNumber>
   </RequestData>
   <RequestData>
      <DODate>2020-02-11</DODate>
      <DONumber>1900200010</DONumber>
      <OrderNumber>3900002603</OrderNumber>
   </RequestData>
   <RequestData>
      <DODate>2020-02-11</DODate>
      <DONumber>1900200011</DONumber>
      <OrderNumber>3900002604</OrderNumber>
      </RequestData>
</ns0:MT_Sales>

I have used below XSLT coding:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>

    <!-- Object or Element Property-->
    <xsl:template match="*">
        "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>
            <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
            <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
            <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="*"/>
    }</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling::*">,</xsl:if>
    </xsl:template>

    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
</xsl:stylesheet>

Output I received:

{        
        "ns0:MT_SAP_SecondarySales" : { "RequestData" :[{

        "DODate" : "2020-02-10",
        "DONumber" : "1900200009",
        "OrderNumber" : "3900002600"
    },{

        "DODate" : "2020-02-11",
        "DONumber" : "1900200011",
        "OrderNumber" : "3900002604"
    }] }}

Output I need:

{"RequestData":[{

        "DODate" : "2020-02-10",
        "DONumber" : "1900200009",
        "OrderNumber" : "3900002600"
    },{

        "DODate" : "2020-02-11",
        "DONumber" : "1900200011",
        "OrderNumber" : "3900002604"
    }] } ```

Help is much appreciated.

It's easier to write a specific stylesheet that fits your structure than adapt a generic one:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/*">
    <xsl:text>{"RequestData":[</xsl:text>
    <xsl:for-each select="*">
        <xsl:text>{&#10;</xsl:text>
        <xsl:for-each select="*">
            <xsl:text>&#9;"</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>" : "</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>"</xsl:text>
            <xsl:if test="position()!=last()">,</xsl:if>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
        <xsl:text>}</xsl:text>
        <xsl:if test="position()!=last()">,</xsl:if>
    </xsl:for-each>
    <xsl:text>]}</xsl:text>
</xsl:template>

</xsl:stylesheet>

All the whitespace characters ( &#9; , &#10; ) are of course optional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM