Rushank
Rushank

Reputation: 27

How to write XSLT for Converting XML to JSON?

I have the following XML File as input and want an JSON output using XSLT mapping. How can I design my XSLT file?

XML Input:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SyncItemMaster versionID="2.14.0" releaseID="9.2"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.14.0/InforOAGIS/BODs/SyncItemMaster.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://schema.infor.com/InforOAGIS/2">
    <ApplicationArea>
        <Sender>
            <LogicalID>lid://infor.ln.ln_1100</LogicalID>
            <ComponentID>erp</ComponentID>
            <ConfirmationCode>OnError</ConfirmationCode>
        </Sender>
        <CreationDateTime>2020-06-16T11:00:16Z</CreationDateTime>
        <BODID>infor-nid:infor:1100::11064973:?ItemMaster&verb=Sync</BODID>
    </ApplicationArea>
    <DataArea>
        <Sync>
            <TenantID>infor</TenantID>
            <AccountingEntityID>1100</AccountingEntityID>
            <LocationID/>
            <ActionCriteria>
                <ActionExpression actionCode="Change"/>
            </ActionCriteria>
        </Sync>
        <ItemMaster>
            <ItemMasterHeader>
                <ItemID>
                    <ID variationID="4241287" lid="lid://infor.ln.ln_1100" accountingEntity="1100">11064973</ID>
                </ItemID>
                <DisplayID>11064973</DisplayID>
                <GTIN/>
                <ServiceIndicator>false</ServiceIndicator>
                <Description>H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="de_DE">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="fr_FR">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="it_IT">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="nl_NL">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="en_US">H3BO3 4% mit Sher Indikator</Description>
                <Note/>
            </ItemMasterHeader>
        </ItemMaster>
    </DataArea>
</SyncItemMaster>

Expected JSON Output:

{ 
   "description":[

      { 
         "languageID":"ge_GE",
         "text":"H3BO3 4% mit Sher Indikator"
      
},
      { 
         "languageID":"de_DE",
         "text":"H3BO3 4% mit Sher Indikator"
},
      {
         "languageID":"fr_FR",
         "text":"H3BO3 4% mit Sher Indikator" 
},
      { 
         "languageID":"it_IT",
         "text":"H3BO3 4% mit Sher Indikator"    
},
      {
         "languageID":"nl_NL",
         "text":"H3BO3 4% mit Sher Indikator"
},
      {
         "languageID":"en_US",
         "text":"H3BO3 4% mit Sher Indikator"
}
   
]
}

Upvotes: 0

Views: 1452

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

In general, in XSLT 3 you have two options, create the JSON as the corresponding XPath 3.1 maps and arrays (https://xsltfiddle.liberty-development.net/pNmCzsG):

<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence
      select="map { 
                'description' : array { 
                  //Description ! 
                  map { 
                    'languageID' : (data(@languageID), 'ge_GE') => head(), 
                    'text' : data() 
                  }
                }
              }"/>
  </xsl:template>
  
</xsl:stylesheet>

or transform the XML input to the XML input format the xml-to-json functions expects (see intermediary result of

<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <map>
        <xsl:where-populated>
            <array key="description">
                <xsl:apply-templates select="//Description"/>
            </array>
        </xsl:where-populated>
    </map>
  </xsl:template>
  
  <xsl:template match="Description[not(@languageID)]">
      <map>
          <string key="languageID">ge_GE</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
  <xsl:template match="Description">
      <map>
          <string key="languageID">{@languageID}</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
</xsl:stylesheet>

) and then use that function xml-to-json (https://xsltfiddle.liberty-development.net/pNmCzsG/1):

<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:variable name="json-xml">
        <map>
            <xsl:where-populated>
                <array key="description">
                    <xsl:apply-templates select="//Description"/>
                </array>
            </xsl:where-populated>
        </map>        
    </xsl:variable>
    <xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="Description[not(@languageID)]">
      <map>
          <string key="languageID">ge_GE</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
  <xsl:template match="Description">
      <map>
          <string key="languageID">{@languageID}</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
</xsl:stylesheet>

The first attempt shown above does not define the order of the JSON property output unless you use a Saxon specific extension attribute. With the second suggestion you could define the order.

Upvotes: 1

Related Questions