Home > Mobile >  Facing issues while converting json to xml though xslt
Facing issues while converting json to xml though xslt

Time:11-26

I am trying to convert JSON data to XML through XSLT using json-to-xml function, I am able to fetch the details of other json. But facing issues in StudentAdress json which has inner json currentAddress and permanentaddress in json request. what can i modify in xslt so that it will convert in xml.

when I am trying to fetch details of StudentAdress though given xslt, i am getting only details of HomeTowndistrict and PinCode, what can i modify in xslt so that i can get details of permanent and current address details.

{
    "envelope": {
        "header": {
            "AddmisionId": "1"
        },
        "body": {
            "StudentRequest": {
                "StudentDetails": {
                    "FirstName": "AMIT",
                    "Telephone": "1234567890",
                    "RollNo": "1"
                },
                "StudentAddress": {
                    "HomeTowndistrict": "BLR",
                    "PermanentAddress": {
                        "addressLine2": "BLR",
                        "addressLine3": "PQR",
                        "addressLine5": "WH"
                    },
                    "CurrentAddress": {
                        "addressLine2": "GYA",
                        "addressLine3": "MNO",
                        "addressLine5": "QW"
                    },
                    "PinCode": "5600"
                },
                "MarkList": {
                    "marks": [
                        {
                            "name": "math",
                            "id": "math80"
                        },
                        {
                            "name": "science",
                            "id": "science123"
                        },
                        {
                            "name": "English",
                            "id": ""
                        },
                        {
                            "name": "Sport",
                            "id": "S23"
                        }
                    ]
                },
                "Report": {
                    "STD": "1",
                    "Code": "QRT90"
                }
            }
        }
    }
}

expected xml

<CLG:StudentRequest
    xmlns:ns0="http://www.w3.org/2005/xpath-functions">
    <CLG:StudentAddress>
        <CLG:HomeTowndistrict>BLR</CLG:HomeTowndistrict>
        <CLG:PermanentAddress>
            <ssp:addressLine1/>
            <ssp:addressLine2>BLR</ssp:addressLine2>
            <ssp:addressLine3>PQR</ssp:addressLine3>
            <ssp:addressLine4/>
            <ssp:addressLine5/>
            <ssp:addressLine5>WH</ssp:addressLine5>
        </CLG:PermanentAddress>
        <CLG:CurrentAddress>
            <ssp:addressLine1/>
            <ssp:addressLine2>GYA</ssp:addressLine2>
            <ssp:addressLine3>MNO</ssp:addressLine3>
            <ssp:addressLine4/>
            <ssp:addressLine5>QW</ssp:addressLine5>
        </CLG:CurrentAddress>
        <CLG:PinCode>5600</CLG:PinCode>
        <CLG:studentUser>
            <CLG:studentUser/></CLG:studentUser>
    </CLG:StudentAddress>
    <CLG:Report>
        <CLG:STD>1</CLG:STD>
        <CLG:Code>QRT90</CLG:Code>
    </CLG:Report>
    <CLG:MarkList>
        <CLG:marks>
            <CLG:name>math</CLG:name>
            <CLG:id>math80</CLG:id>
        </CLG:marks>
        <CLG:marks>
            <CLG:name>science</CLG:name>
            <CLG:id>science123</CLG:id>
        </CLG:marks>
        <CLG:marks>
            <CLG:name>English</CLG:name>
            <CLG:id/>
        </CLG:marks>
        <CLG:marks>
            <CLG:name>Sport</CLG:name>
            <CLG:id>S23<CLG:id/>
        </CLG:marks>
    </CLG:MarkList>
    <CLG:StudentDetails>
        <FirstName>AMIT</FirstName>
        <Telephone>1234567890</Telephone>
        <RollNo>1</RollNo>
    </CLG:StudentDetails>
</CLG:StudentRequest>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:CLG="http://www.w3.org/2005/xpath-functions"
    xmlns:ns0="http://www.w3.org/2005/xpath-functions"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ssp="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="fn" expand-text="yes">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" omit-xml-declaration="yes"
        indent="yes" />

    <xsl:param name="jsonText" />

    <xsl:template name="init">
        <CLG:StudentRequest xmlns="##namespace##">

            <xsl:apply-templates
                select="json-to-xml($jsonText)" />

        </CLG:StudentRequest>
    </xsl:template>

    <xsl:template match="fn:map[@key = 'Report']">
        <CLG:Report>
            <CLG:STD>{fn:string[@key = 'STD']}</CLG:STD>
            <CLG:Code>{fn:string[@key = 'Code']}</CLG:Code>
        </CLG:Report>
    </xsl:template>

    <xsl:template match="fn:array[@key = 'marks']">
        <CLG:MarkList>
            <xsl:iterate select="*">
                <CLG:marks>
                    <CLG:name>{fn:string[@key = 'name']}</CLG:name>
                    <CLG:id>{fn:string[@key = 'id']}</CLG:id>
                </CLG:marks>
            </xsl:iterate>
        </CLG:MarkList>
    </xsl:template>

    <xsl:template match="fn:map[@key = 'StudentAddress']">
        <CLG:StudentAddress>
            <CLG:PermanentAddress>
                <ssp:addressLine1>{fn:string[@key = 'addressLine1']}</ssp:addressLine1>
                <ssp:addressLine2>{fn:string[@key = 'addressLine2']}</ssp:addressLine2>
                <ssp:addressLine3>{fn:string[@key = 'addressLine3']}</ssp:addressLine3>
                <ssp:addressLine4>{fn:string[@key = 'addressLine4']}</ssp:addressLine4>
                <ssp:addressLine5>{fn:string[@key = 'addressLine5']}</ssp:addressLine5>
            </CLG:PermanentAddress>
            <CLG:CurrentAddress>
                <ssp:addressLine1>{fn:string[@key = 'addressLine1']}</ssp:addressLine1>
                <ssp:addressLine2>{fn:string[@key = 'addressLine2']}</ssp:addressLine2>
                <ssp:addressLine3>{fn:string[@key = 'addressLine3']}</ssp:addressLine3>
                <ssp:addressLine4>{fn:string[@key = 'addressLine4']}</ssp:addressLine4>
                <ssp:addressLine5>{fn:string[@key = 'addressLine5']}</ssp:addressLine5>
            </CLG:CurrentAddress>
            <CLG:HomeTowndistrict>{fn:string[@key = 'StudentAddress']}
            </CLG:HomeTowndistrict>
            <CLG:PinCode>{fn:string[@key = 'PinCode']}</CLG:PinCode>
            <CLG:studentUser>
                <CLG:studentUser>{fn:string[@key = 'studentUser']}</CLG:studentUser>
            </CLG:studentUser>
        </CLG:StudentAddress>
    </xsl:template>

    <xsl:template match="fn:map[@key = 'StudentDetails']">
        <CLG:StudentDetails>
            <FirstName>{fn:string[@key = 'FirstName']}</FirstName>
            <Telephone>{fn:string[@key = 'Telephone']}</Telephone>
            <RollNo>{fn:string[@key = 'RollNo']}</RollNo>
        </CLG:StudentDetails>
    </xsl:template>
</xsl:stylesheet>

CodePudding user response:

Your path lack a map[@key = '..'] selection so

        <CLG:PermanentAddress>
            <ssp:addressLine1>{fn:map[@key = 'PermanentAddress']/fn:string[@key = 'addressLine1']}</ssp:addressLine1>
            <ssp:addressLine2>{fn:map[@key = 'PermanentAddress']/fn:string[@key = 'addressLine2']}</ssp:addressLine2>
            <ssp:addressLine3>{fn:map[@key = 'PermanentAddress']/fn:string[@key = 'addressLine3']}</ssp:addressLine3>
            <ssp:addressLine4>{fn:map[@key = 'PermanentAddress']/fn:string[@key = 'addressLine4']}</ssp:addressLine4>
            <ssp:addressLine5>{fn:map[@key = 'PermanentAddress']/fn:string[@key = 'addressLine5']}</ssp:addressLine5>
        </CLG:PermanentAddress>
        <CLG:CurrentAddress>
            <ssp:addressLine1>{fn:map[@key = 'CurrentAddress']/fn:string[@key = 'addressLine1']}</ssp:addressLine1>
            <ssp:addressLine2>{fn:map[@key = 'CurrentAddress']/fn:string[@key = 'addressLine2']}</ssp:addressLine2>
            <ssp:addressLine3>{fn:map[@key = 'CurrentAddress']/fn:string[@key = 'addressLine3']}</ssp:addressLine3>
            <ssp:addressLine4>{fn:map[@key = 'CurrentAddress']/fn:string[@key = 'addressLine4']}</ssp:addressLine4>
            <ssp:addressLine5>{fn:map[@key = 'CurrentAddress']/fn:string[@key = 'addressLine5']}</ssp:addressLine5>
        </CLG:CurrentAddress>

should fix those address values.

I haven't tried to check whether something else is missing.

  • Related