Using Saxon XSLT 3.0 I am trying to convert XML to JSON. The first step is to convert the input XML into the required Maps & Array format the xml-to-json() required format.
When I then call the function with
<xsl:result-document href="{$target_json}" method="json">
<xsl:copy-of select="xml-to-json($intermediate_XML, map{., 'indent':false() })"></xsl:copy-of>
</xsl:result-document>
Sample XML:
<?xml version="1.0" encoding="US-ASCII"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="headline">Matilda sold such dreadful ties</string>
<string key="body">
<p>Matilda sold such dreadful ties.</p>
<p>It made Juan retch and...</p>
</string>
</map>
JSON Result
"\n { \"headline\" : \"Matilda sold such dreadful ties\",\n \"body\" : \"\\n<p>Matilda sold such dreadful ties.<\\\/p>\\n<p>It made Juan retch and...<\\\/p>\\n\" }"
What I expected and need is
{ "headline" : "Matilda sold such dreadful ties", "body" : "\n<p>Matilda sold such dreadful ties.<\/p>\n<p>It made Juan retch and...<\/p>\n" }
Without the Starting and ending quote " and all the escaped characters.
Any assistance would be most welcome, thanks.
I don't think you want both output method as
jsonand also usexml-to-json, use output methodtextfor the result of that function.Example using Saxon HE 11.
Another example using Saxon HE 11 in an Azure backend, this time with xsl:result-document.
The output method
jsonis useful if you represent your data as XDM 3.1 maps/arrays e.g.Online example.