Re: [xsl] XML to Json and escaped characters

Subject: Re: [xsl] XML to Json and escaped characters
From: "dvint dvint@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Mar 2025 20:07:01 -0000
Thanks, that did the trick, I was seeing it appear as text so wasn't think of
it as tabs and newlines.Sent from my Verizon, Samsung Galaxy smartphone
-------- Original message --------From: "Michael Kay michaelkay90@xxxxxxxxx"
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: 3/18/25  11:00 AM  (GMT-08:00)
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Subject: Re: [xsl] XML to Json
and escaped characters The regular expression `\t` matches tab characters. To
match the escape sequence `\t`, you want the regular expression `\\t`.Michael
KaySaxonica> On 18 Mar 2025, at 17:56, dvint@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:> > 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:> >B B B B  {
"code" : 99,>B B B B B  "actionMessage" : "Please
click\n\t\t\t\t\t\t\t\t\t\tto Continue.",>B B B B B  "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 referencesB B  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 -->>B B B  <xsl:function
name="j:escape" as="xs:string" visibility="final">>B B B B B B B  <xsl:param
name="in" as="xs:string"/>>B B B B B B B 
<xsl:value-of>>B B B B B B B B B B B  <xsl:for-each
select="string-to-codepoints($in)">>B B B B B B B B B B B B B B B 
<xsl:choose>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". gt
65535">>B B B B B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="concat('\u', j:hex4((. - 65536) idiv 1024 +
55296))"/>>B B B B B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="concat('\u', j:hex4((. - 65536) mod 1024 +
56320))"/>>B B B B B B B B B B B B B B B B B B B 
</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
34">\"</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
92">\\</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
08">\b</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
09">\t</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
10">\n</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
12">\f</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". =
13">\r</xsl:when>>B B B B B B B B B B B B B B B B B B B  <xsl:when test=". lt
32 or (. ge 127 and . le 160)">>B B B B B B B B B B B B B B B B B B B B B B B 
<xsl:value-of select="concat('\u',
j:hex4(.))"/>>B B B B B B B B B B B B B B B B B B B 
</xsl:when>>B B B B B B B B B B B B B B B B B B B 
<xsl:otherwise>>B B B B B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="codepoints-to-string(.)"/>>B B B B B B B B B B B B B B B B B B B 
</xsl:otherwise>>B B B B B B B B B B B B B B B 
</xsl:choose>>B B B B B B B B B B B  </xsl:for-each>>B B B B B B B 
</xsl:value-of>>B B B  </xsl:function>> > ..dan> >

Current Thread