I am trying to create an XSLT to transform my XML into a specific format that then gets transformed into JSON.
My sample file looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<RegResults>
<RegInfo>
<tradingPartnerId>12345</tradingPartnerId>
<ediTransactionCode>204</ediTransactionCode>
</RegInfo>
<RegInfo>
<tradingPartnerId>12345</tradingPartnerId>
<ediTransactionCode>214</ediTransactionCode>
</RegInfo>
<RegInfo>
<tradingPartnerId>23456</tradingPartnerId>
<ediTransactionCode>204</ediTransactionCode>
</RegInfo>
<RegInfo>
<tradingPartnerId>23456</tradingPartnerId>
<ediTransactionCode>214</ediTransactionCode>
</RegInfo>
</RegResults>
After the XSLT transformation, my output XML should look like this:
<tradingPartnerId>12345</tradingPartnerId>
<ediTransactionCode>204</ediTransactionCode>
<ediTransactionCode>214</ediTransactionCode>
<tradingPartnerId>23456</tradingPartnerId>
<ediTransactionCode>204</ediTransactionCode>
<ediTransactionCode>214</ediTransactionCode>
The goal is to ultimately transform the XML in the previous transformation into the following JSON (which is done by a service in my application):
{
"tradingPartnerId": "12345",
"ediTransactionCodes": [
"204",
"214"
]
"tradingPartnerId": "23456",
"ediTransactionCodes": [
"204",
"214"
]
}
I have to use XSLT 1.0. I tried using 'distinct-values' in an online formatter, which I believe is an XSLT 2.0 function, nevertheless, I am not receiving the XML output I need. Here is the XSLT I tried, but clearly it is wrong:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="distinct-values(//*[local-name()='tradingPartnerId'])">
<tradingPartnerId>
<xsl:value-of select="."/>
<xsl:for-each select="distinct-values(//*[local-name()='ediTransactionCode'])">
<ediTransactionCode>
<xsl:value-of select="."/>
</ediTransactionCode>
</xsl:for-each>
</tradingPartnerId>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Any assistance is appreciated.