Just to clarify, I am using XSLT 1.0. Sorry for not specifying that at first.
I have an XSLT stylesheet where I'd like to replace double quotes with something safe that's safe to go into a JSON string. I'm trying to do something like the following:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/message">
<xsl:variable name="body"><xsl:value-of select="body"/></xsl:variable>
{
"message" :
{
"body": "<xsl:value-of select="normalize-space($body)"/>"
}
}
</xsl:template>
</xsl:stylesheet>
If I have XML passed in that looks like the following, this will always work just fine:
<message>
<body>This is a normal string that will not give you any issues</body>
</message>
However, I'm dealing with a body that has full blown HTML in it, which isn't an issue because normalize-space()
will take care of the HTML, but not double quotes. This breaks me:
<message>
<body>And so he quoted: "I will break him". The end.</body>
</message>
I really don't care if the double quotes are HTML escaped or prefixed with a backslash. I just need to make sure the end result passes a JSON parser.
This output passes JSON Lint and would be an appropriate solution (backslashing the quotes):
{ "body" : "And so he quoted: \"I will break him\". The end." }