1

I have a scenario where in I am trying to convert the following XML:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <ClientId>1023</ClientId>
    <PackageId>1542</PackageId>
    <LegalEntityId>30</LegalEntityId>
    <title>Mr</title>
    <firstName>kumar</firstName>
    <middleName></middleName>
    <lastName>kumar</lastName>
    <contactDetails>
        <emailIds>
            <emailIdType>Primary</emailIdType>
            <emailId>[email protected]</emailId>
        </emailIds>
        <contactNumber>
            <contactType>Mobile</contactType>
            <candidateContact>9999999999</candidateContact>
        </contactNumber>
    </contactDetails>
    <EICCaseDocuments>
        <DocumentType>AuthorizationForm</DocumentType>
        <fileContent>PDF URL</fileContent>
        <fileType>1</fileType>
    </EICCaseDocuments>
    <checks>
        <CheckId>28</CheckId>
        <Source>Graduation</Source>
        <checkFields>
            <Name>Registration number/Seat number </Name>
            <Value>Value</Value>
        </checkFields>
        <checkFields>
            <Name>Mode of Study: Full time / Part time / Distance learning course</Name>
            <Value>Value</Value>
        </checkFields>
        <files>
            <DocumentName>Document Name</DocumentName>
            <fileContent>PDF URL</fileContent>
            <fileType>1</fileType>
        </files>
    </checks>
    <checks>
        <CheckId>30</CheckId>
        <Source>Previous</Source>
        <checkFields>
            <Name>Employee code</Name>
            <Value>Value</Value>
        </checkFields>
        <checkFields>
            <Name>Designation</Name>
            <Value>Value</Value>
        </checkFields>
        <files>
            <DocumentName>Document Name</DocumentName>
            <fileContent>PDF URL</fileContent>
            <fileType>1</fileType>
        </files>
    </checks>
</root>

Into a JSON Payload that looks like the following:

{
  "ClientId": 1023,
  "PackageId": 1542,
  "LegalEntityId": 30,
  "title": "Mr",
  "firstName": "kumar",
  "middleName": "",
  "lastName": "kumar",
  "contactDetails": {
    "emailIds": [
      {
        "emailIdType": "Primary",
        "emailId": "[email protected]"
      }
    ],
    "contactNumber": [
      {
        "contactType": "Mobile",
        "candidateContact": "9999999999"
      }
    ]
  },
    "EICCaseDocuments": [
        {
            "DocumentType": "AuthorizationForm",
            "fileContent": "<PDF URL>",
            "fileType": "1"
        }
    ],
  "checks": [
    {
      "CheckId": 28,
      "Source": "Graduation",
      "checkFields": [
        {
          "Name": "Registration number/Seat number ",
          "Value": "Value"
        },
        {
          "Name": "Mode of Study: Full time / Part time / Distance learning course",
          "Value": "Value"
        }
      ],
      "files": [{
          "DocumentName": "<Document Name>",
          "fileContent": "<PDF URL>",
          "fileType": "1"
        }]
    },
    {
      "CheckId": 30,
      "Source": "Previous",
      "checkFields": [
        {
          "Name": "Employee code",
          "Value": "Value"
        },
        {
          "Name": "Designation",
          "Value": "Value"
        }
      ],
      "files": [{
          "DocumentName": "<Document Name>",
          "fileContent": "<PDF URL>",
          "fileType": "1"
        }]
    }
  ]
}

As you can see, every node is an array and there are even some fields whose values are not enclosed in quotes.

I am trying to achieve this on SAP CPI using XSLT Mapping. I have played around with the following code but I am not getting the desired output. Please find my code below:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://use your namespace">
<xsl:output method="text"/>
<xsl:template match="/root">{
    <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: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>

Through this code, I am getting "checks" and "checkFields" the way i require.

However, I am unable to enclose "emailIds","contactNumber","EICCaseDocuments" and any "files" nodes in an array. Also, how can i remove the quotes enclosing ClientId, Packageid, LegalEntity and CheckId?

I understand that this is a generic code. But really need some guidance on how to achieve the answers to both my queries above.

Could really use some help on this.

Thanks!

3
  • Please ask a specific question - and reduce the code to the minimum necessary to demonstrate the specific problem. Commented Dec 12, 2022 at 10:52
  • Thank you for your suggestion - I have made the changes. Commented Dec 13, 2022 at 11:42
  • Does this help? stackoverflow.com/questions/74534791/… Commented Jun 23, 2023 at 15:20

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.