0
<root><dataResponse>{"status":"C","responseCode":200,"description":"Success","resultsInformation":[{"sourceSystemAppID":"8257000008880","sourceSystemCustomerID":"82570000021453","sourceSystemName":"FINCOR","customerType":"AI","recordType":"INPUT","name":"TAMIZHARASI  M  ","employerName":"","dateOfBirth":"08-10-1977","panNo":"","voterID":"GSDF2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"ressAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"permAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM   ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"permAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM   ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"}],"emailInformation":[{"emailType":"ressEmail","emailID":""}],"contactInformation":[{"phoneType":"offPhone","phone":""},{"phoneType":"mobile","phone":"9659306719"},{"phoneType":"ressPhone","phone":""}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"0000","sourceSystemCustomerID":"82570000021453","sourceSystemName":"FINCOR","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M  ","employerName":"","dateOfBirth":"08-10-1977","panNo":"","voterID":"FEF2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"ressAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"}],"emailInformation":[{"emailType":"others","emailID":""}],"contactInformation":[{"phoneType":"offPhone","phone":""},{"phoneType":"ressPhone","phone":""},{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"3181919","sourceSystemCustomerID":"1","sourceSystemName":"FINCOR","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M  ","employerName":"","dateOfBirth":"08-10-1977","panNo":"","voterID":"GFG2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"ressAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"}],"emailInformation":[{"emailType":"ressEmail","emailID":""}],"contactInformation":[{"phoneType":"offPhone","phone":""},{"phoneType":"ressPhone","phone":""},{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"3181919","sourceSystemCustomerID":"358166","sourceSystemName":"FINCOR","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M  ","employerName":"","dateOfBirth":"08-10-1977","panNo":"","voterID":"EYT2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"ressAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"}],"emailInformation":[{"emailType":"ressEmail","emailID":""}],"contactInformation":[{"phoneType":"offPhone","phone":""},{"phoneType":"ressPhone","phone":""},{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"570000021422","sourceSystemCustomerID":"82570000021422","sourceSystemName":"BRNET","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M","employerName":"","dateOfBirth":"10-08-1977","panNo":"","voterID":"TYR2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"others","address":"pirni","city":"verna","state":"goa","pincode":"403722"}],"emailInformation":[{"emailType":"others","emailID":"[email protected]"}],"contactInformation":[{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"570000021453","sourceSystemCustomerID":"82570000021452","sourceSystemName":"BRMF","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M","employerName":"","dateOfBirth":"10-08-1977","panNo":"","voterID":"SGSG2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"others","address":"pirni","city":"verna","state":"goa","pincode":"403722"}],"emailInformation":[{"emailType":"others","emailID":"[email protected]"}],"contactInformation":[{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"570000021453","sourceSystemCustomerID":"82570000021453","sourceSystemName":"BRMF","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M","employerName":"","dateOfBirth":"10-08-1977","panNo":"","voterID":"TYR2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"others","address":"pirni","city":"verna","state":"goa","pincode":"403722"}],"emailInformation":[{"emailType":"others","emailID":"[email protected]"}],"contactInformation":[{"phoneType":"mobile","phone":"9999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"82570000021453","sourceSystemCustomerID":"82570000021453","sourceSystemName":"BRMF","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M","employerName":"","dateOfBirth":"10-08-1977","panNo":"","voterID":"Gtyv2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"others","address":"pirni","city":"verna","state":"goa","pincode":"403722"}],"emailInformation":[{"emailType":"others","emailID":"[email protected]"}],"contactInformation":[{"phoneType":"mobile","phone":"9999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"82570000021453","sourceSystemCustomerID":"82570000021856","sourceSystemName":"BRMF","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M","employerName":"","dateOfBirth":"10-08-1977","panNo":"","voterID":"YRV2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[],"emailInformation":[],"contactInformation":[{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""},{"sourceSystemAppID":"8257000008881","sourceSystemCustomerID":"82570000021454","sourceSystemName":"FINCOR","customerType":"AI","recordType":"ONLINE","name":"TAMIZHARASI  M  ","employerName":"","dateOfBirth":"08-10-1977","panNo":"","voterID":"YRV2613891","passportNo":"","drivingLicense":"","aadhaarNumber":"","gstNumber":"","registrationORCINNo":"","rationCard":"","nregaCard":"","nationalIdNo":"","tanNo":"","gender":"F","addressInformation":[{"addressType":"ressAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"},{"addressType":"offAddr","address":"49/2 NEW KRISHNAPURAM UYYAKONDAVARI VIRUDHACHALAM  ","city":"VIRUDHACHALAM","state":"TAMILNADU","pincode":"607802"}],"emailInformation":[{"emailType":"ressEmail","emailID":""}],"contactInformation":[{"phoneType":"offPhone","phone":""},{"phoneType":"ressPhone","phone":""},{"phoneType":"mobile","phone":"999999999"}],"ckycNumber":"","chassisNumber":"","engineNumber":"","vehicleRegistrationNo":"","remarks":""}]}</dataResponse><message>Request Completed Successfully</message><statusCode>000</statusCode><status>Success</status></root>

This is my xml code.I have written xslt code so far

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cs="urn:cs" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" exclude-result-prefixes="msxsl cs">
   <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
   <msxsl:script language="C#" implements-prefix="cs">
      <msxsl:assembly name="System.Web.Extensions" />
      <msxsl:using namespace="System.Collections.Generic" />
      <msxsl:using namespace="System.Web.Script.Serialization" />
      <![CDATA[public string getXMLFromJson(string jsonString){
      string resp="";
      var serializer = new JavaScriptSerializer();
      Dictionary<string, object> result = (serializer.DeserializeObject(jsonString) as Dictionary<string, object>);    
      foreach(string key in result.Keys){
        var newKey=key.Replace(" ","_");
        resp+="<"+newKey+">"+result[key]+"</"+newKey+">";
      }  
            return resp;
    }]]>
   </msxsl:script>
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="root/dataResponse">
      <xsl:variable name="jstr">
         <xsl:value-of select="." />
      </xsl:variable>
      <dataResponse>
         <xsl:value-of select="cs:getXMLFromJson($jstr)" disable-output-escaping="yes" />
      </dataResponse>
   </xsl:template>
</xsl:stylesheet>

I have not able to loop through nested object resultsInformation and also want loop addressInformation and convert same into xml.My Current output is as shown below.

    <root>
  <dataResponse><status>C</status><responseCode>200</responseCode><description>Success</description><resultsInformation>System.Object[]</resultsInformation></dataResponse>
  <message>Request Completed Successfully</message>
  <statusCode>000</statusCode>
  <status>Success</status>
</root>
3
  • I didn't even know you could inject C# code in xslt... Can you post the code to make that franken-xslt run? Commented Jan 1, 2021 at 17:48
  • What I can suggest is that you "extract" the C# code, at least for debugging it... In the end you know what the input is (the json) and what the output should be (an xml fragment). The xslt is only additional baggage that whill make everything more complex to debug. Commented Jan 1, 2021 at 17:53
  • If you can use json.net it becomes easy enough: stackoverflow.com/questions/18503845/… Commented Jan 1, 2021 at 17:58

1 Answer 1

1

If you can use Json.Net it becomes a one-liner:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cs="urn:cs" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" exclude-result-prefixes="msxsl cs">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*"/>
    <msxsl:script language="C#" implements-prefix="cs">
        <msxsl:assembly name="System.Xml.Linq" />
        <msxsl:using namespace="Newtonsoft.Json" />
        <msxsl:assembly name="Newtonsoft.Json" />
        <msxsl:using namespace="System.Xml.Linq" />
        <![CDATA[
        public static XPathNavigator getXMLFromJson(string jsonString)
        {
            var xml = JsonConvert.DeserializeXmlNode(jsonString, "dataResponse");
            return xml.CreateNavigator();
        }
        ]]>
    </msxsl:script>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="root/dataResponse">
        <xsl:copy-of select="cs:getXMLFromJson(.)" />
    </xsl:template>
</xsl:stylesheet>

Note that I've added a

<xsl:strip-space elements="*"/>

to format correctly the resulting xml.

Without Json.Net it is much more complex (and I'm not sure all the corner-cases are covered):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cs="urn:cs" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" exclude-result-prefixes="msxsl cs">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*"/>
    <msxsl:script language="C#" implements-prefix="cs">
        <msxsl:assembly name="System.Core" />
        <msxsl:assembly name="System.Xml.Linq" />
        <msxsl:assembly name="System.Web.Extensions" />
        <msxsl:using namespace="System.Collections.Generic" />
        <msxsl:using namespace="System.Linq" />
        <msxsl:using namespace="System.Xml.Linq" />
        <msxsl:using namespace="System.Web.Script.Serialization" />
        <![CDATA[
            public static XPathNavigator XmlFromJson(string root, string json)
            {
                var jss = new JavaScriptSerializer();
                var xml = ToXElement(root, (IDictionary<string, object>)jss.DeserializeObject(json));
                return xml.CreateNavigator();
            }

            public static XElement ToXElement(string name, IDictionary<string, object> coll)
            {
                if (coll == null)
                {
                    return new XElement(name, null);
                }

                return new XElement(
                    name,
                    coll.Select(x =>
                        x.Value is IDictionary<string, object> ? new[] { ToXElement(x.Key, (IDictionary<string, object>)x.Value) } :
                        x.Value is object[]? ToXElement(x.Key, (object[])x.Value) :
                        new[] { new XElement(x.Key, x.Value) }
                    ).SelectMany(x => x)
                    .ToArray());
            }

            public static XElement[] ToXElement(string name, object[] coll)
            {
                if (coll == null)
                {
                    return new[] { new XElement(name, null) };
                }

                return Array.ConvertAll(coll, x =>
                    x is IDictionary<string, object> ? ToXElement(name, (IDictionary<string, object>)x) :
                    x is object[]? new XElement(name, ToXElement(name, (object[])x)) :
                    new XElement(name, x)
                );
            }
        ]]>
    </msxsl:script>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="root/dataResponse">
        <xsl:copy-of select='cs:XmlFromJson("dataResponse", .)' />
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

I can't use Newtonsoft.Json to deserialize
Updated code is working for me.I will check it for multiple scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.