0
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Oracle BI Publisher -Dataengine, datamodel:_Custom_OAL_ATG_OM_Dashboard_DM_xdm -->
<DATA>
<SUCCESS>
<COUNT___>5686</COUNT___>
</SUCCESS>
<REJECT>
<COUNT___>641</COUNT___>
</REJECT>
<FAILURE>
<COUNT___>8536</COUNT___>
</FAILURE>
<ERROR>
<COUNT___>1447</COUNT___>
</ERROR>
<TERMINATED>
<COUNT___>1341</COUNT___>
</TERMINATED>
</DATA>

Above is the XML I have. I want to convert the above XML into JSON shown below.

{
"appName": "PERFORMANCE",
"statsName": "Status Counts",
"DateBegin": "xxxxxx",
"DateEnd": "xxxxxxx",
"data": {
 "SUCCESS ": 1341,
"REJECT":5666,  "FAILURE":640,
 "ERROR":8515,
"TERMINATED":1447
}
}

I am new with XSLT stylesheets. Could anyone help me with the above problem? What is the XSLT stylesheet for converting the given XML to JSON?

  • Duplicated https://stackoverflow.com/questions/45739445/xml-to-json-transformation-in-xslt-3-0 – Alejandro Mar 12 '19 at 20:38
  • 1
    @Alejandro: It is not a duplicate because the given question requests an XSLT-1.0 solution and your duplicate consists of an XSLT-3.0 solution. – zx485 Mar 12 '19 at 20:47
  • @zx485 There are also a lot of answer about XML to JSON conversion in the context of XSLT 1.0. All of them dealing with topics your answer is lacking. – Alejandro Mar 12 '19 at 20:59

1 Answers1

2

You can use the following stylesheet. It works as desired for the given input XML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
<xsl:output method="text" />

  <xsl:template match="/DATA">
      <xsl:text>{
    "appName": "PERFORMANCE",
    "statsName": "Status Counts",
    "DateBegin": "xxxxxx",
    "DateEnd": "xxxxxxx",
    "data": 
      </xsl:text>{&#xa;<xsl:apply-templates select="*" /><xsl:text>&#xa;}&#xa;}</xsl:text>
  </xsl:template>

  <xsl:template match="SUCCESS|REJECT|FAILURE|ERROR|TERMINATED">
    <xsl:value-of select="concat('&quot;',local-name(),'&quot;: ',COUNT___)" />
    <xsl:if test="position() != last()">,&#xa;</xsl:if>
  </xsl:template>

</xsl:stylesheet>

Its output is:

{
"appName": "PERFORMANCE",
"statsName": "Status Counts",
"DateBegin": "xxxxxx",
"DateEnd": "xxxxxxx",
"data": 
{
"SUCCESS": 5686,
"REJECT": 641,
"FAILURE": 8536,
"ERROR": 1447,
"TERMINATED": 1341
}
}

The output doesn't match your desired output, because there were some inconsistencies between the input XML and the desired output XML. Change the XSLT according to your needs.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • thanks for the answer. This is what I am looking for. I have one other quick question. Is there anyway, I could get the DateBegin and DateEnd in Unix epoch format rather than xxxx? DateEnd should be current time in XSLT and DateBegin should be 10 minutes before the DateEnd. Any help is appreciated. I am new to this. – Karthik Gullapalli Mar 12 '19 at 20:55
  • 1
    @KarthikGullapalli: Please ask a new question as a question rather than a comment. Thank you. – kjhughes Mar 12 '19 at 20:56
  • @KarthikGullapalli: This is a whole new question. You didn't even specify the values of it (currently they are marked with 'xxxx'). – zx485 Mar 12 '19 at 20:57
  • @zx485 Yeah I wanted to add it as a new question but I didn't know if it would fit into this or add it as a whole new question. However, could you help me figure out how I can fetch the time? – Karthik Gullapalli Mar 12 '19 at 21:00
  • @KarthikGullapalli: You cannot fetch the time with XSLT-1.0. Hence you should ask a new question. – zx485 Mar 12 '19 at 21:02