I've recently started working with json-to-xxml() to create a conversion
to asciidoc format for documentation. I've now found a need to take some
of that generated XML back to JSON as it is an actual example for an API
call.
Here is an XML snippet:
<map key="actionCodes">
<number key="code">99</number>
<string key="actionMessage">Please click
to Continue.</string>
<string key="cat">Environment</string>
</map>
That produces this:
{ "code" : 99,
"actionMessage" : "Please click\n\t\t\t\t\t\t\t\t\t\tto
Continue.",
"cat" : "Environment" },
In the actionMessage I have all the \t and \n values that I need to
normalize. I've got other places where URLs have
"url" :
"https:\/\/cfweb.amalgam.ascendant.cloud\/MWA\/index.html?token=c9c11bd1-5cdd-48d9-993a-fa061e050380"
}
or text with the / escaped
"ScoreSegmentResult" : "N\/A",
I tried something like this to remove the \t
<xsl:variable name="rawJSON" select="j:xml-to-json(., map { 'indent' :
true() })"/>
<xsl:value-of select="replace($rawJSON, '\t', '')"/>
But it doesn't make any difference to the output.
Any thoughts on how to handle this?
I checked the spec for xml-to-json() that references using custom
template rules for xml-tojson() and references B.2 Stylesheet for
converting XML to JSON. In that stylesheet I see the section I would
want to change, but I have no clue on how I would go about using this
suggestion.
This is what I need to change
<!-- Function to escape special characters -->
<xsl:function name="j:escape" as="xs:string" visibility="final">
<xsl:param name="in" as="xs:string"/>
<xsl:value-of>
<xsl:for-each select="string-to-codepoints($in)">
<xsl:choose>
<xsl:when test=". gt 65535">
<xsl:value-of select="concat('\u', j:hex4((. -
65536) idiv 1024 + 55296))"/>
<xsl:value-of select="concat('\u', j:hex4((. -
65536) mod 1024 + 56320))"/>
</xsl:when>
<xsl:when test=". = 34">\"</xsl:when>
<xsl:when test=". = 92">\\</xsl:when>
<xsl:when test=". = 08">\b</xsl:when>
<xsl:when test=". = 09">\t</xsl:when>
<xsl:when test=". = 10">\n</xsl:when>
<xsl:when test=". = 12">\f</xsl:when>
<xsl:when test=". = 13">\r</xsl:when>
<xsl:when test=". lt 32 or (. ge 127 and . le 160)">
<xsl:value-of select="concat('\u', j:hex4(.))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="codepoints-to-string(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:value-of>
</xsl:function>
..dan