Search code examples
jsonxsltxslt-2.0xslt-groupingxslt-3.0

Square bracket for single & multiple LineItems in xml to json


How to get square bracket or Array for single or multiple 'Details' lineitems. Need to get the square bracket or Array [ even for single line 'Details' too. we have more than 1 line 'Details'. The requirement is to populate the [ even from single line 'Details'.

xmlFile for single Line 'Details':

<root>
    <FirstName>Alex</FirstName>
    <LastName>Fin</LastName>
    <Details>
        <Id_Number>111</Id_Number>
        <Location>NC</Location>
        <Contact>
             <PhoneNumber>+1 323</PhoneNumber>
         </Contact>
    </Details>
</root>

ExpectedJsonFile:

     {
    "FirstName": "Alex",
    "LastName": "Fin",
    "Details": [
      {
        "Id_Number": 111,
        "Location": "NC",
        "Contact": {
          "PhoneNumber": "+1 323"
        }
      }
    ]
 }

xml File with multiple Line 'Details:

<root>
        <FirstName>Alex</FirstName>
        <LastName>Fin</LastName>
        <Details>
            <Id_Number>111</Id_Number>
            <Location>NC</Location>
            <Contact>
                 <PhoneNumber>+1 323</PhoneNumber>
             </Contact>
        </Details>
        <Details>
            <Id_Number>222</Id_Number>
            <Location>TX</Location>
            <Contact>
                 <PhoneNumber>+1 323</PhoneNumber>
             </Contact>
        </Details>
        <Address>
            <Locality>Urban</Locality>
            <Distance>
               <Miles>2</Miles>
            </Distance>
            <Commute>Yes</Commute>
         </Address>
         <Address>
            <Locality>Rular</Locality>
            <Distance>
               <Miles>1</Miles>
            </Distance>
            <Commute>Yes</Commute>
         </Address>
    </root>

Expected Jsonfile for multiple LineItems'Details'

    {
    "FirstName": "Alex",
    "LastName": "Fin",
    "Details": [
      {
        "Id_Number": 111,
        "Location": "NC",
        "Contact": {
          "PhoneNumber": "+1 323"
        }
      },
      {
        "Id_Number": 222,
        "Location": "TX",
        "Contact": {
          "PhoneNumber": "+1 323"
        }
      }
    ],
    "Address": [
      [
          {
        "Locality": "Urban",
        "Distance": {
          "Miles": 2
        },
        "Commute": "Yes"
      }
      ],
      [
          {
        "Locality": "Rular",
        "Distance": {
          "Miles": 1
        },
        "Commute": "Yes"
      }
      ]
    ]
  }

xsltCode:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:variable name="json-xml">
          <xsl:apply-templates/>
      </xsl:variable>
      <xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="*[not(*)]">
    <string key="{local-name()}">{.}</string>
  </xsl:template>
  
  <xsl:template match="*[(*) and . castable as xs:double]">
    <number key="{local-name()}">{.}</number>
  </xsl:template>
  
  <xsl:template match="*[*]">
    <xsl:param name="key" as="xs:boolean" select="false()"/>
    <map>
      <xsl:if test="$key">
        <xsl:attribute name="key" select="local-name()"/>
      </xsl:if>
      <xsl:for-each-group select="*" group-by="node-name()">
          <xsl:choose>
              <xsl:when test="current-group()[2] or self::Details or self::Contact">
                  <array key="{local-name()}">
                    <xsl:choose>
                      <xsl:when test="self::Address">
                        <array>
                          <xsl:apply-templates select="current-group()">
                            <xsl:with-param name="key" select="false()"/>
                          </xsl:apply-templates>                        
                        </array>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:apply-templates select="current-group()">
                          <xsl:with-param name="key" select="false()"/>
                        </xsl:apply-templates>
                      </xsl:otherwise>                      
                    </xsl:choose>
                  </array>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()">
                    <xsl:with-param name="key" select="true()"/>
                  </xsl:apply-templates>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
    </map>
  </xsl:template>

</xsl:stylesheet>

Solution

  • The code is generating an array when the group of like-named nodes contains two or more items:

    <xsl:when test="current-group()[2]">
        <array key="{local-name()}">
            <xsl:apply-templates select="current-group()"/>
        </array>
    </xsl:when>
    

    so the simplest fix would be to use the same logic when the element name is "Details":

    <xsl:when test="current-group()[2] or self::Details">
    

    Or a better design might be to make it data-driven; have a global variable containing the list of element names for which you want to force array output, and test the element for membership in this list.