0

I would like to use XSLT to transform some XML into JSON.

The XML looks like the following:

<root>
 <item>
   <number>123</number>
   <name>ABC</name>
   <EmployeeCode>
   <id>11</id>
   <Department>
         <Name>Zing</Name>
         <Age>42</Age>
         <Designation>Engineer</Designation>
    </Department>
    <Status>Contractor</Status>
   </EmployeeCode>
*Expecting like below*
<root>
 <item>
    <number>123</number>
    <name>ABC</name>
    <EmployeeCode>
        <id>11</id>
        "Department": {"Name": "Zing","Age": "42","Designation": "Engineer"}
        <Status>Contractor</Status>
    </EmployeeCode>
</item>
Venkat
  • 1
  • Which XSLT version, which XSLT processor do you use or can you use? With XSLT 3 it is easier than in previous versions to do it in a short and robust way. – Martin Honnen Aug 06 '21 at 16:20
  • Please ask a **specific** question about a difficulty you encountered when trying to accomplish this. Otherwise it looks like you're just looking for someone to write your code for you. Also, in all your questions regarding XSLT please state which version of XSLT your processor supports. – michael.hor257k Aug 06 '21 at 16:23
  • why don't you try wildcard on department/* and in for loop consider generating json string. – Gaurav Aug 06 '21 at 16:27
  • Looking closer it also is not quite clear at which point you want or have JSON in the output, the part with `"Department" : ` doesn't look like quite like JSON. – Martin Honnen Aug 06 '21 at 16:27
  • I want to convert the Department structure into Json formate, the Main Structure should be in XML only, – Venkat Aug 06 '21 at 17:43
  • XSLT 3 with XPath 3.1 allows you to create JSON and serialize to JSON with two approaches, either by transforming your XML to the format the `xml-to-json` function expects or by using the `serialize` function with `method` being `json` on XPath 3.1 maps/arrays you create from the XML you have. – Martin Honnen Aug 06 '21 at 19:59
  • can you provide an example, please.. – Venkat Aug 07 '21 at 04:29
  • You will first need to tell or decide whether you can use XSLT 3 with e.g. Saxon 10 or Saxon-JS 2 or Altova XML. As for examples, reading https://www.w3.org/TR/xslt-30/#json helps, as should https://www.w3.org/TR/xpath-functions/#maps-and-arrays. Then look at similar questions like https://stackoverflow.com/questions/62927952/how-to-convert-xml-to-json-in-xslt/62931081#62931081 or https://stackoverflow.com/questions/48947872/xslt-3-0-json-to-xml-and-xml-to-json-conversion. – Martin Honnen Aug 07 '21 at 07:31

0 Answers0