1

Possible Duplicate:
convert xml to jsonx using xslt

Can anyone help me out in getting the below with an array..I have to generate the generalised xsl.. 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>

Output:

<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>

I am using the below xslt and output but not as expected above.Could any one please help me.

<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="*">
            <json:object name="{name()}">
                <xsl:apply-templates/>
            </json:object>
        </xsl:template>
        <!-- String -->
        <xsl:template match="*[not(*)]">
            <json:string name="{name()}">
                <xsl:value-of select="."/>
            </json:string>
        </xsl:template>
    </xsl:stylesheet>
Community
  • 1
  • 1
user1731504
  • 159
  • 1
  • 3
  • 12

1 Answers1

1

May be you meant to say that Groups should be an array and Group should be an object instead of the other way around?

Try this...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
  exclude-result-prefixes="xsl xs">
<xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" /> 

<xsl:template match="/">
  <json:object>
     <xsl:apply-templates />    
  </json:object>
</xsl:template>

<!-- Array -->
<xsl:template match="*[*[2]][name(*[1])=name(*[2])]">
  <json:array name="{name()}">
    <xsl:apply-templates />
  </json:array>
</xsl:template>

<!-- Object -->  
<xsl:template match="*">
  <json:object name="{name()}">
    <xsl:apply-templates />
  </json:object>
</xsl:template>

<!-- String -->
<xsl:template match="*[not(*)]">
  <json:string name="{name()}">
    <xsl:value-of select="." />
  </json:string>
</xsl:template>
</xsl:stylesheet>

...the output is this...

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
   <json:object name="Login">
      <json:array name="Groups">
         <json:object name="Group">
            <json:string name="Name">john</json:string>
            <json:string name="Password"/>
         </json:object>
         <json:object name="Group">
            <json:string name="Name">john</json:string>
            <json:string name="Password"/>
         </json:object>
      </json:array>
   </json:object>
</json:object>

Update

In response to the OP's updated requirements, here is a new solution.

<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 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="*">
  <json:object name="{name()}">
    <xsl:apply-templates />
  </json:object>
</xsl:template>

<!-- String -->
<xsl:template match="*[not(*)]">
  <json:string name="{name()}">
    <xsl:value-of select="." />
  </json:string>
</xsl:template>
</xsl:stylesheet>

Output is so...

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
  <json:object name="Login">
    <json:object name="Groups">
      <json:array name="Group">
        <json:object>
          <json:string name="Name">john</json:string>
          <json:string name="Password"></json:string>
        </json:object>
        <json:object>
          <json:string name="Name">john</json:string>
          <json:string name="Password"></json:string>
        </json:object>
      </json:array>
    </json:object>
  </json:object>
</json:object>
Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
  • @SeanB.Durkin---Thanks for the reply I need the Gropus should be as object and Group should be as an array... – user1731504 Oct 17 '12 at 14:18
  • @user1731504 Are you sure? That doesn't make a lot of sense? – Sean B. Durkin Oct 17 '12 at 14:23
  • Your rule is an inversion of common sense. But if you are sure, I can adjust. – Sean B. Durkin Oct 17 '12 at 14:24
  • Anyway, what was the problem with Luke's solution? – Sean B. Durkin Oct 17 '12 at 14:25
  • So why did you tag the question version 2.0 ? – Sean B. Durkin Oct 17 '12 at 14:31
  • Initially I was told that 2.0 is fine so I tagged but later on I came to know that we require only in 1.0 but not 2.0... – user1731504 Oct 17 '12 at 14:38
  • @Swan B.Durkin--Thanks for the updated solution... I have to test for all the scenarios..I'll test it and get abck to you.. – user1731504 Oct 18 '12 at 08:35
  • @Swan B.Durkin--- I am posting again because the updated xsl is not working as expected for the xml that I had updated in the question.. Could you please look into it.I had mentioned the expected output also..account,balances,properties should be as array but showing as object's... Could anyone please help me.. – user1731504 Nov 14 '12 at 07:21
  • Could any one please help me? – user1731504 Nov 14 '12 at 09:38
  • My answer was the correct answer to your original question and fitted your use case perfectly. If you substantially change the use case, it is new information and really a new question. Please accept this answer and post a new question. You can insulate yourself against the risk of need of further question updates by providing broad coverage of your use cases. Try to make your use case cover many possible conditions. – Sean B. Durkin Nov 15 '12 at 00:02
  • B.Durkin --- I had posted the new question..Please check this link.. http://stackoverflow.com/questions/13375360/need-help-in-writing-common-xsl – user1731504 Nov 15 '12 at 03:06