0

I am trying to transform XML payload to jsonx format. My code is not working when input XML has attributes, could you please assist. Application element should be coming as json:object but it is coming as json:string May I know how can I keep Applicaton element as json:object ?

below is the code I am trying: i used below to code test

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/"  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" version="1.0"> 
    <xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="*[local-name()='Envelope']">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*[local-name()='Body']">
        <json:object name="Body">
            <xsl:apply-templates/>
        </json:object>
    </xsl:template>
    <!-- Array -->
    <xsl:template match="*[*[2]][name(*[1]) = name(*[2])]">
        <json:object name="{local-name()}">
            <json:array name="{local-name(*[1])}">
                <xsl:apply-templates/>
            </json:array>
        </json:object>
    </xsl:template>
    <!-- Array member -->
    <xsl:template match="*[parent::*[ name(*[1])=name(*[2]) ]] | /">
        <json:object>
            <xsl:apply-templates/>    
        </json:object>
    </xsl:template>
    <!-- Object -->
    <xsl:template match="*">
        <json:object name="{local-name()}">     
            <xsl:apply-templates select="@*|node()"/>
        </json:object>
    </xsl:template>
    <!-- String -->
    <xsl:template match="*[not(*)]">
        <json:string name="{local-name()}">
            <xsl:value-of select="."/>          
            <xsl:apply-templates select="@*"/>          
        </json:string>  
    </xsl:template>

    <xsl:template match="@*">
        <json:string name="{local-name()}">
            <xsl:value-of select="."/>                  
        </json:string>     
    </xsl:template> 

</xsl:stylesheet>

Input XML: input payload xml which i am using

 <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <SendEmailResponse xmlns="http://webservices.abcd.com/">
                <SendEmailResult>
                    <EmailSpecifications xmlns="">
                        <Status EmailID="0" Success="N">
                            <ErrorCode>1010</ErrorCode>
                            <ErrorDescription>Invalid AppID</ErrorDescription>
                        </Status>
                        <Application AppID="0" EntityID="0" />
                    </EmailSpecifications>
                </SendEmailResult>
            </SendEmailResponse>
        </soap:Body>
    </soap:Envelope>

output jsonx what I am getting now: expecting <json:object name="Application"> in the output but getting <json:string name="Application">

<json:object
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
>

    <json:object name="Body">
        <json:object name="SendEmailResponse">
            <json:object name="SendEmailResult">
                <json:object name="EmailSpecifications">
                    <json:object name="Status">
                        <json:string name="EmailID">0</json:string>
                        <json:string name="Success">N</json:string>
                        <json:string name="ErrorCode">1010</json:string>
                        <json:string name="ErrorDescription">Invalid AppID</json:string>
                    </json:object>
                    <json:string name="Application">
                        <json:string name="AppID">0</json:string>
                        <json:string name="EntityID">0</json:string>
                    </json:string>
                </json:object>
            </json:object>
        </json:object>
    </json:object>

</json:object>

Expected OutPut: need to change<json:string name="Application"> to <json:object name="Application"> in the output XML

    <json:object
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
>

    <json:object name="Body">
        <json:object name="SendEmailResponse">
            <json:object name="SendEmailResult">
                <json:object name="EmailSpecifications">
                    <json:object name="Status">
                        <json:string name="EmailID">0</json:string>
                        <json:string name="Success">N</json:string>
                        <json:string name="ErrorCode">1010</json:string>
                        <json:string name="ErrorDescription">Invalid AppID</json:string>
                    </json:object>
                    <json:object name="Application">
                        <json:string name="AppID">0</json:string>
                        <json:string name="EntityID">0</json:string>
                    </json:object>
                </json:object>
            </json:object>
        </json:object>
    </json:object>

</json:object>
sarma
  • 63
  • 1
  • 12
  • Is your input xml correct? – Ed Bangga Aug 21 '19 at 02:28
  • hi metal, thanks for looking into my query. I have updated my xsl and also question little bit, could you please check again. I have tried few things and came close to what I was looking for – sarma Aug 21 '19 at 03:23

1 Answers1

1
<xsl:template match="*[not(*) and not(@*)]">

Or Use

<xsl:template match="*[not(*) and string-length(.) &gt; 0]">

Instead of your String Template

<xsl:template match="*[not(*)]">
Sam
  • 841
  • 1
  • 5
  • 15