I have to generate XMl output. It should display arrays as shown later. I am unable to render the arrays in the proper way.
Input XML:
<accounts>
<displayOrdinal>0</displayOrdinal>
<name>String</name>
<account>
<accountNumber>String</accountNumber>
<name>String</name>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<usage>
<type>String</type>
</usage>
<usage>
<type>String</type>
</usage>
</account>
<account>
<accountNumber>String</accountNumber>
<name>String</name>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<usage>
<type>String</type>
</usage>
<usage>
<type>String</type>
</usage>
</account>
</accounts>
My expected output should be as follows:
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<json:object name="accounts">
<json:string name="displayOrdinal">0</json:string>
<json:string name="name">String</json:string>
<json:array name="account">
<json:object>
<json:string name="accountNumber">String</json:string>
<json:string name="name">String</json:string>
<json:array name="balance">
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
</json:array>
<json:array name="properties">
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
</json:array>
<json:array name="usage">
<json:object>
<json:string name="type">String</json:string>
</json:object>
<json:object name="usage">
<json:string name="type">String</json:string>
</json:object>
</json:array>
</json:object>
<json:object>
<json:string name="accountNumber">String</json:string>
<json:string name="name">String</json:string>
<json:object name="balance">
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:array name="balance">
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
</json:array>
<json:array name="usage">
<json:object>
<json:string name="type">String</json:string>
</json:object>
<json:object>
<json:string name="type">String</json:string>
</json:object>
</json:array>
</json:object>
</json:array>
</json:object>
</json:object>
The XSL that I am using is as below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- Array -->
<xsl:template match="*[*[2]][name(*[1])=name(*[2])]">
<json:object name="{name()}">
<json:array name="{name(*[1])}">
<xsl:apply-templates/>
</json:array>
</json:object>
</xsl:template>
<!-- Array member -->
<xsl:template match="*[parent::*[ name(*[1])=name(*[2]) ]] | /">
<json:object>
<xsl:apply-templates/>
</json:object>
</xsl:template>
<!-- Object -->
<xsl:template match="*">
<xsl:choose>
<xsl:when test="text()">
<json:string name="{name()}">
<xsl:value-of select="."/>
</json:string>
</xsl:when>
<xsl:otherwise>
<json:object name="{name()}">
<xsl:apply-templates/>
</json:object>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- String -->
<xsl:template match="*[not(*)]">
<xsl:choose>
<xsl:when test="not(boolean(text()))">
<xsl:element name="json:null">
<xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="text()= 'false' or text()='true'">
<xsl:element name="json:boolean">
<xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<json:string name="{name()}">
<xsl:if test="@*">
<xsl:attribute name="{name(@*)}"><xsl:value-of select="@*"/></xsl:attribute>
</xsl:if>
<xsl:value-of select="."/>
</json:string>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I think you will need a number of templates for this. Firstly, for the array elements, you will need to match elements who have different preceding sibling, but a matching-named following sibling (i.e. they are the first element of the array)
The priority is used here because in the final XSLT, there will be two templates that could potential match the same element, and the array one needs to be chosen first.
Then you can process the elements in the array by outputing the element as an json object, and then matching the following sibling but only if it has the name name.
There would also need to be a template to stop elements in the array being matched independently of the array processing so that the don't get output twice.
The other templates needed will be one for string elements, which are elements with no child elements
And finally one to match other elements, which would just be output as objects.
Here is the full XSLT
When applied to your XML, the following is output
Congrats to Tim for first correct solution.
My solution is not any better, but I present it as a matter of interest. It produces the same output as Tim's, plus I have incorporated the null and boolean types suggested by the OP's original solution attempt.