skip to Main Content

I have a Soap XML which contains multiple records. I am working on fetching the data of tags ReportId, PolicyNumber etc. Now this records also has multiple "records" tag within "sf:Case_Responses_GCC_r" tag. I need to create an XSLT file which could help me in transforming this SOAP XML into JSON data. I have done this using XSLT 1.0 but as I am using saxon-js which supports XSLT 3.0 processor.

My XML Data

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns="urn:enter.soap.force.com"
  xmlns:sf="urn:sobject.enter.soap.force.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <queryMoreResponse>
      <result>
        <done>false</done>
        <records xsi:type="sf:Case">
          <sf:ReportId>6896SDRGrt868</sf:ReportId>
          <sf:PolicyNumber>445353566</sf:PolicyNumber>
           <sf:Case_Responses_GCC__r>
             <records xsi:type="sf:Response_GCC__c">
               <sf:Id xsi:nil="true"/>
               <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
               <sf:Response_GCC__c>Yes</sf:Response_GCC__c>
             </records>
             <records xsi:type="sf:Response_GCC__c">
              <sf:Id xsi:nil="true"/>
              <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
              <sf:Response_GCC__c>Self</sf:Response_GCC__c>
             </records>
           </sf:Case_Responses_GCC__r>
        </records>
        <records xsi:type="sf:Case">
          <sf:ReportId>5003L005UCcfVVS</sf:ReportId>
          <sf:PolicyNumber>87768978</sf:PolicyNumber>
           <sf:Case_Responses_GCC__r>
             <records xsi:type="sf:Response_GCC__c">
               <sf:Id xsi:nil="true"/>
               <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
               <sf:Response_GCC__c>No</sf:Response_GCC__c>
             </records>
             <records xsi:type="sf:Response_GCC__c">
              <sf:Id xsi:nil="true"/>
              <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
              <sf:Response_GCC__c>Father</sf:Response_GCC__c>
             </records>
           </sf:Case_Responses_GCC__r>
        </records>
      </result>
    </queryMoreResponse>
  </soapenv:Body>
</soapenv:Envelope>

I have tried using this XSLT but its throwing error with used functions

XSLT I have tried

<xsl:stylesheet version="3.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sf="urn:sobject.enter.soap.force.com"
xpath-default-namespace="urn:enter.soap.force.com"
xmlns:json="http://www.w3.org/2005/xpath-functions/" 
expand-text="yes"
exclude-result-prefixes="#all">

    <xsl:output method="json" version="1.0" encoding="utf-8" indent="yes"/>

    <xsl:template match="/soapenv:Envelope">
        <xsl:variable name="jsonObject">
            <lic lang="en">
                <xsl:apply-templates select="soapenv:Body/queryMoreResponse/result/records"/>
            </lic>
        </xsl:variable>
        <xsl:value-of select="json:serialize($jsonObject, map { 'method': 'json', 'indent': true() })"/>
    </xsl:template>
    
    <xsl:template match="sf:records">
        <xsl:map>
            <xsl:map-entry key="reportversion" select="1"/>
            <xsl:map-entry key="reportid" select="sf:ReportId"/>
            <xsl:map-entry key="PolicyNumber" select="sf:PolicyNumber"/>
            <xsl:map-entry key="primarysourcecountry" select="'IN'"/>
            <xsl:map-entry key="client">
                <xsl:map>
                    <xsl:map-entry key="summary">
                        <xsl:variable name="responses">
                            <xsl:apply-templates select="sf:Case_Responses_GCC__r/records"/>
                        </xsl:variable>
                        <xsl:sequence select="json:to-array($responses)"/>
                    </xsl:map-entry>
                </xsl:map>
            </xsl:map-entry>
        </xsl:map>
    </xsl:template>
    
    <xsl:template match="sf:records">
        <xsl:map>
            <xsl:map-entry key="sf:Question_GCC__c" select="sf:Question_GCC__c"/>
            <xsl:map-entry key="sf:Response_GCC__c" select="sf:Response_GCC__c"/>
        </xsl:map>
    </xsl:template>

</xsl:stylesheet>

Expected JSON Output

{
    "lic":[
        {
            "reportversion":"1",
            "reportid":"5003L000005UCcfQAG",
            "PolicyNumber": "004176451",
            "primarysourcecountry": "IN",
            "client":{
                "summary": {
                    "Available to question?": "No",
                    "Relationship": "Father"
                }
            }
        },
        {
            "reportversion":"1",
            "reportid":"5003L000005UCcfQAG",
            "PolicyNumber": "004176451",
            "primarysourcecountry": "IN",
            "client":{
                "summary": {
                    "Available to question?": "No",
                    "Relationship": "Father"
                }
            }
        }       
    ]
}

2

Answers


  1. The following should get you closer:

    <xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sf="urn:sobject.enter.soap.force.com"
    xpath-default-namespace="urn:enter.soap.force.com"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all">
      
      <xsl:function name="mf:process-records" as="item()*">
        <xsl:param name="elements" as="element()*"/>
        <xsl:apply-templates select="$elements"/>
      </xsl:function>
    
        <xsl:output method="json" indent="yes"/>
    
        <xsl:template match="/soapenv:Envelope">
          <xsl:map>
            <xsl:map-entry key="'lic'" select="array { mf:process-records(soapenv:Body/queryMoreResponse/result/records) }"/>
          </xsl:map>
        </xsl:template>
        
        <xsl:template match="records">
            <xsl:map>
                <xsl:map-entry key="'reportversion'" select="1"/>
                <xsl:map-entry key="'reportid'" select="string(sf:ReportId)"/>
                <xsl:map-entry key="'PolicyNumber'" select="string(sf:PolicyNumber)"/>
                <xsl:map-entry key="'primarysourcecountry'" select="'IN'"/>
                <xsl:map-entry key="'client'">
                    <xsl:map>
                        <xsl:map-entry key="'summary'">
                          <xsl:map>
                            <xsl:apply-templates select="sf:Case_Responses_GCC__r/records"/>
                          </xsl:map>
                        </xsl:map-entry>
                    </xsl:map>
                </xsl:map-entry>
            </xsl:map>
        </xsl:template>
        
        <xsl:template match="sf:Case_Responses_GCC__r/records">
          <xsl:map-entry key="sf:Question_GCC__c" select="string(sf:Response_GCC__c)"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    There are various ways to create XDM/XPath/XSLT maps to be serialized as JSON, the suggestion above tries to follow your intention to use xsl:map and xsl:map-entry, using just XPath 3.1 expression for XDM maps and arrays might be a bit more compact:

    <xsl:stylesheet version="3.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:sf="urn:sobject.enter.soap.force.com"
      xpath-default-namespace="urn:enter.soap.force.com"
      xmlns:map="http://www.w3.org/2005/xpath-functions/map"
      exclude-result-prefixes="#all">
      
      <xsl:output method="json" indent="yes"/>
    
      <xsl:template match="/soapenv:Envelope">
        <xsl:sequence
          select="map { 'lic' : array {
                     soapenv:Body/queryMoreResponse/result/records ! map {
                       'reportversion' : 1,
                       'reportid' : string(sf:ReportId),
                       'PolicyNumber' : string(sf:PolicyNumber),
                       'primarysourcecountry' : 'IN',
                       'client' : map {
                         'summary' : map:merge(
                           sf:Case_Responses_GCC__r/records ! map:entry(sf:Question_GCC__c, string(sf:Response_GCC__c))
                         )
                       }
                     }
                  }}"/>
      </xsl:template>
    
    </xsl:stylesheet>
    
    Login or Signup to reply.
  2. you can also use camaro for this

    const { transform } = require('camaro')
    const fs = require('fs')
    
    async function main() {
        const input = fs.readFileSync('input.xml', 'utf-8')
        const template = {
            lic: ['//result/records', {
                reportID: 'sf:ReportId',
                policyNumber: 'sf:PolicyNumber',
                client: {
                    summary: {
                        availableToQuestion: 'sf:Case_Responses_GCC__r/records/sf:Question_GCC__c[text() = "Available to question?"]/../sf:Response_GCC__c',
                        relationship: 'sf:Case_Responses_GCC__r/records/sf:Question_GCC__c[text() = "Relationship"]/../sf:Response_GCC__c'
                    }
                }
            }]
        }
        const result = await transform(input, template)
    
        console.log(JSON.stringify(result, null, 2))
    }
    
    main()
    

    output

    {
      "lic": [
        {
          "reportID": "6896SDRGrt868",
          "policyNumber": "445353566",
          "client": {
            "summary": {
              "availableToQuestion": "Yes",
              "relationship": "Self"
            }
          }
        },
        {
          "reportID": "5003L005UCcfVVS",
          "policyNumber": "87768978",
          "client": {
            "summary": {
              "availableToQuestion": "No",
              "relationship": "Father"
            }
          }
        }
      ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search