JSON to XML with XSL fails in Oxygen but works online

Having trouble installing Oxygen? Got a bug to report? Post it all here.
ianluk
Posts: 2
Joined: Fri Oct 29, 2021 12:51 pm

JSON to XML with XSL fails in Oxygen but works online

Post by ianluk »

Hi
I'm trying to set up a transformation in the Oxygen editor to convert JSON files into DITA topics, but my XSL keeps failing in the Oxygen editor with the error:
SXXP0003: Content is not allowed in prolog.
However, the online JSON->XML conversion worked at https://www.oxygenxml.com/xml_json_converter.html

In the Oxygen editor, I tried using the JSON content as a variable in the XSL to avoid input file issues, but when the XSL is run the error occurs. I also tried with files with various formats, and without BOM, but nothing's worked.

I've seen in online threads that there's something in the Oxygen editor that might be causing this.

Can someone help?

Regards,
Ian
json-xsl-test-files.zip
(915 Bytes) Downloaded 101 times
ianluk
Posts: 2
Joined: Fri Oct 29, 2021 12:51 pm

Re: JSON to XML with XSL fails in Oxygen but works online

Post by ianluk »

Update...

After more time banging my head against the wall, I made some progress.

1) Wrap the JSON with an XML element, eg <data>

Code: Select all

<data>[
    {
        "name": "org",
        "order": 1,
        "displayName": "Organization"
    },
    {
	"name": "usr",
        "order": 2,
        "displayName": "User"
        },
    {
	"name": "zeb",
        "order": 3,
        "displayName": "Zebra"
        }
]
</data>
2) In the XSL, make the initial template to match the XML element; eg: (first successful test)

<xsl:template match="data">
<xsl:copy-of select="json-to-xml(.)"/>
</xsl:template>

This output the XML:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<array xmlns="http://www.w3.org/2005/xpath-functions">
   <map>
      <string key="name">org</string>
      <number key="order">1</number>
      <string key="displayName">Organization</string>
   </map>
   <map>
      <string key="name">usr</string>
      <number key="order">2</number>
      <string key="displayName">User</string>
   </map>
   <map>
      <string key="name">zeb</string>
      <number key="order">3</number>
      <string key="displayName">Zebra</string>
   </map>
</array>
So now I have an XML file of array and map elements, I can hopefully take the next step toward DITA. (fingers crossed!)
tavy
Posts: 365
Joined: Thu Jul 01, 2004 12:29 pm

Re: JSON to XML with XSL fails in Oxygen but works online

Post by tavy »

Hello Ian,

The json-to-xml() function parameter must be the JSON file content. When you load a JSON file content you need to use the unparsed-text() function. Something like this

Code: Select all

 <xsl:variable name="json-xml" select="json-to-xml(unparsed-text($input))"/>
You can find an example of XSL file that converts JSON to XML in our samples folder [OxygenInstallDir]/samples/json/transform/json-to-xml.xsl

There is also a webinar and a vide demo that shows how to convert JSON to XML using XSLT:
https://www.oxygenxml.com/demo/json_query.html
https://youtu.be/wRgvWEKo6_w?t=2374

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply