0
votes

I need to make XSLT which will generete json.

The problem is that i have &lt; and &gt; symbols in my xml which need to be replaced with <> in output. But disable-output-escaping="yes" makes line break, can i remove this line break?

XML:

<item id="166" group="0">
<name>Some Titile</name>
<text>&lt;p&gt;A dizzying array of deep, robust color, the Hugs and Kisses bouquet contains one dozen tulips and one dozen iris, fresh from the fields. It's an always-impressive bouquet perfect for a &quot;dinner for two,&quot; a special someone's birthday, or anytime you want to make someone's heart race.&lt;/p&gt;</text>
</item>

Output which I need

{
    "name": "Some Title",
    "text": "<p>A dizzying array of deep, robust color, the Hugs and Kisses bouquet contains one dozen tulips and one dozen iris, fresh from the fields. It's an always-impressive bouquet perfect for a &quot;dinner for two,&quot; a special someone's birthday, or anytime you want to make someone's heart race.</p>;"
}

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:template match="item">
    {
    "name": "<xsl:value-of select="name" />",
    "text": "<xsl:value-of select="text" />"
    }
  </xsl:template>
</xsl:stylesheet>

Output

{
    "name": "Some Title",
    "text": "&lt;p&gt;A dizzying array of deep, robust color, the Hugs and Kisses bouquet contains one dozen tulips and one dozen iris, fresh from the fields. It's an always-impressive bouquet perfect for a &quot;dinner for two,&quot; a special someone's birthday, or anytime you want to make someone's heart race&lt;./p&gt;;"
}
2
Please, edit the question and provide enough information so that everyone could repro the problem. In addition to the XML document and the wanted result, we need: 1. The completer XSLT code (as small as possible) and, 2. the actual result you are getting.Dimitre Novatchev

2 Answers

0
votes

Well if you want to generate Javascript code respectively JSON code then simply use <xsl:output method="text"/>, that way no characters in the tranformation result will be escaped and there certainly then is no need to use disable-output-escaping.

On the other hand I don't see why disable-output-escaping should yield line breaks so if you still have problems consider to show us the details like the XML input, the XSLT code, the result you want and the one you get, and which XSLT processor you use.

0
votes

If you are generating JSON, don't use disable-output-escaping, use the text output method:

<xsl:output method="text"/>

With the text output method, there is no escaping, therefore no need to disable it.

I don't know if this solves the problem with newlines, since I don't know what's causing that problem.