1

Trying to convert xml to json using generic xslt

Input xml:

<Response>
<Info>
    <Domain/>
    <Source/>
</Info>
<OrderDetails itemNo="1 ">
    <OrderNum>0002060412</OrderNum>
    <sourceType>UTD</sourceType>
    <organization>UTD09</organization>
    <AmtAdjust>Not Known-Need Coding</AmtAdjust>
</OrderDetails>
<OrderDetails itemNo="2 ">
    <OrderNum>0002060415</OrderNum>
    <sourceType>UTD</sourceType>
    <organization>UTD09</organization>
    <AmtAdjust>Not Known-Need Coding</AmtAdjust>
</OrderDetails>
<OrderDetails itemNo="3 ">
    <OrderNum>0002060413</OrderNum>
    <sourceType>UTD</sourceType>
    <organization>UTD09</organization>
    <AmtAdjust>Not Known-Need Coding</AmtAdjust>
</OrderDetails>
<OrderDetails itemNo="5 ">
    <OrderNum>0060064425</OrderNum>
    <sourceType>UTD</sourceType>
    <organization>UTD09</organization>
    <TickNums>
        <TktNum>11776564428635</TktNum>
    </TickNums>
    <LineDetail>
        <itemNum>000010</itemNum>
        <type>ftr</type>
        <amnt>100.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <LineDetail>
        <itemNum>000020</itemNum>
        <type>ert</type>
        <amnt>30.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <LineDetail>
        <itemNum>000030</itemNum>
        <type>VAT</type>
        <amnt>10.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <LineDetail>
        <itemNum>000040</itemNum>
        <type>C2</type>
        <amnt>20.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <AmtAdjust>Not Known-Need Coding</AmtAdjust>
</OrderDetails>
<OrderDetails itemNo="6 ">
    <OrderNum>0060064427</OrderNum>
    <sourceType>UTD</sourceType>
    <organization>UTD09</organization>
    <TickNums>
        <TktNum>11234569354874</TktNum>
    </TickNums>
    <TickNums>
        <TktNum>45465749854377</TktNum>
    </TickNums>
    <LineDetail>
        <itemNum>000010</itemNum>
        <type>BR</type>
        <amnt>100.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <LineDetail>
        <itemNum>000020</itemNum>
        <type>ytr</type>
        <amnt>100.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <LineDetail>
        <itemNum>000030</itemNum>
        <type>COMM</type>
        <amnt>200.000000000 </amnt>
        <qty>1.000 </qty>
        <center>0002/0002</center>
    </LineDetail>
    <AmtAdjust>Not Known-Need Coding</AmtAdjust>
</OrderDetails>

My common xslt:

  <?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:with-param name="parent" select="'Yes'"> </xsl:with-param>
        </xsl:call-template>
    </xsl:template>

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

    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:param name="parent"></xsl:param>
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>            
            <xsl:when test="not(*|@*)"><xsl:choose><xsl:when test="$parent='Yes'"> <xsl:text>&quot;</xsl:text><xsl:value-of select="."/><xsl:text>&quot;</xsl:text></xsl:when>
                    <xsl:otherwise>"<xsl:value-of select="name()"/>":"<xsl:value-of  select="."/>"</xsl:otherwise>
                </xsl:choose>           
            </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>

But the output is as follows:

{    
    "Response" :{            
    "Info" :{            
    "Domain" :"",
    "Source" :""
            },
    "OrderDetails" :{
            "itemNo" : "1 ",
    "OrderNum" :"0002060412",
    "sourceType" :"UTD",
    "organization" :"UTD09",
    "AmtAdjust" :"Not Known-Need Coding"
            },
    "OrderDetails" :{
            "itemNo" : "2 ",
    "OrderNum" :"0002060415",
    "sourceType" :"UTD",
    "organization" :"UTD09",
    "AmtAdjust" :"Not Known-Need Coding"
            },
    "OrderDetails" :{
            "itemNo" : "3 ",
    "OrderNum" :"0002060413",
    "sourceType" :"UTD",
    "organization" :"UTD09",
    "AmtAdjust" :"Not Known-Need Coding"
            },
    "OrderDetails" :{
            "itemNo" : "5 ",
    "OrderNum" :"0060064425",
    "sourceType" :"UTD",
    "organization" :"UTD09",
    "TickNums" :{            
    "TktNum" :"11776564428635"
            },
    "LineDetail" :{            
    "itemNum" :"000010",
    "type" :"ftr",
    "amnt" :"100.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "LineDetail" :{            
    "itemNum" :"000020",
    "type" :"ert",
    "amnt" :"30.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "LineDetail" :{            
    "itemNum" :"000030",
    "type" :"VAT",
    "amnt" :"10.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "LineDetail" :{            
    "itemNum" :"000040",
    "type" :"C2",
    "amnt" :"20.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "AmtAdjust" :"Not Known-Need Coding"
            },
    "OrderDetails" :{
            "itemNo" : "6 ",
    "OrderNum" :"0060064427",
    "sourceType" :"UTD",
    "organization" :"UTD09",
    "TickNums" :{            
    "TktNum" :"11234569354874"
            },
    "TickNums" :{

    "TktNum" :"45465749854377"
            },
    "LineDetail" :{            
    "itemNum" :"000010",
    "type" :"BR",
    "amnt" :"100.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "LineDetail" :{            
    "itemNum" :"000020",
    "type" :"ytr",
    "amnt" :"100.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "LineDetail" :{            
    "itemNum" :"000030",
    "type" :"COMM",
    "amnt" :"200.000000000 ",
    "qty" :"1.000 ",
    "center" :"0002/0002"
            },
    "AmtAdjust" :"Not Known-Need Coding"
            }
            }}

The outout I'm expecting is

    {
    "Response": {
        "Info": {
            "Domain": "",
            "Source": ""
        },
        "OrderDetails": [
            {
                "itemNo": "1 ",
                "OrderNum": "0002060412",
                "sourceType": "UTD",
                "organization": "UTD09",
                "AmtAdjust": "Not Known-Need Coding"
            },
            {
                "itemNo": "2 ",
                "OrderNum": "0002060415",
                "sourceType": "UTD",
                "organization": "UTD09",
                "AmtAdjust": "Not Known-Need Coding"
            },
            {
                "itemNo": "3 ",
                "OrderNum": "0002060413",
                "sourceType": "UTD",
                "organization": "UTD09",
                "AmtAdjust": "Not Known-Need Coding"
            },
            {
                "itemNo": "5 ",
                "OrderNum": "0060064425",
                "sourceType": "UTD",
                "organization": "UTD09",
                "TickNums": {
                    "TktNum": ["11776564428635"]
                },
                "LineDetail": [
                    {
                        "itemNum": "000010",
                        "type": "ftr",
                        "amnt": "100.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    },
                    {
                        "itemNum": "000020",
                        "type": "ert",
                        "amnt": "30.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    },
                    {
                        "itemNum": "000030",
                        "type": "VAT",
                        "amnt": "10.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    },
                    {
                        "itemNum": "000040",
                        "type": "C2",
                        "amnt": "20.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    }
                ],
                "AmtAdjust": "Not Known-Need Coding"
            },
            {
                "itemNo": "6 ",
                "OrderNum": "0060064427",
                "sourceType": "UTD",
                "organization": "UTD09",
                "TickNums": {
                    "TktNum": [
                        "11234569354874",
                        "45465749854377"
                    ]
                },
                "LineDetail": [
                    {
                        "itemNum": "000010",
                        "type": "BR",
                        "amnt": "100.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    },
                    {
                        "itemNum": "000020",
                        "type": "ytr",
                        "amnt": "100.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    },
                    {
                        "itemNum": "000030",
                        "type": "COMM",
                        "amnt": "200.000000000 ",
                        "qty": "1.000 ",
                        "center": "0002/0002"
                    }
                ],
                "AmtAdjust": "Not Known-Need Coding"
            }
        ]
    }
}

But the input xml can't be changed to add more fields to populate arrays. So how to use the existing xslt as more like a generic one.

2
  • Curious why do you need this conversion done in XSLT? Any general-purpose language (PHP, Python, R, Java, C#, VB) can parse XML in a container and parse back out in JSON. XSLT at end of day is a special-purpose language.
    – Parfait
    Commented Apr 12, 2017 at 0:59
  • @Parfait ... because XSLT is a special-purpose language designed for exactly this kind of job, it can often do the task with a fraction of the code needed in languages like Java or PHP; and the code will be more maintainable. Commented Apr 15, 2018 at 9:00

1 Answer 1

1

If the stylesheet is going to generate an array for TktNums, then it needs some extra information beyond what's available from your input XML. It needs some kind of directive to tell it that TktNums can be repeated, even though it isn't repeated in this particular input. You could put this information in the stylesheet, but that would thwart your aim of making the stylesheet completely generic. Alternatively you could get it from a schema, or from some kind of configuration file of your own invention. But it's got to come from somewhere.

In the case of LineDetail, you could generate an array based on the fact that the child element repeats. But that would give you problems in the case of an input instance where LineDetail only occurs once.

Basically, this is not a coding problem but a design/specification problem.

1
  • Thanks explained the same to client
    – mnvbrtn
    Commented Apr 17, 2017 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.