Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace new line char with <br /> XSL
    primarykey
    data
    text
    <p>I have a Sharepoint list which I want to convert to a JSON via an XSL dataview.</p> <p>I have an XSL recursive replace function which I use to replace all special characters (escape backslash, double quotes to &amp;quot; etc) which gives me nice clean JSON which parses correctly in the users browser.</p> <p>The final thing that I need to escape / replace is the new line char. The new line causes errors in parsing the JSON in some browsers.</p> <p>Here is some xsl which tests if the contents of title has a new line char, if it does we output a paragraph:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="/"&gt; &lt;xsl:for-each select="catalog/cd"&gt; &lt;xsl:if test='contains(title,"&amp;#xA;")'&gt; &lt;p&gt;Found &lt;xsl:value-of select="title" /&gt;&lt;/p&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Here is some sample xml:</p> <pre><code>&lt;catalog&gt; &lt;cd&gt; &lt;title&gt;Empire Burlesque&lt;/title&gt; &lt;artist&gt;Bob Dylan&lt;/artist&gt; &lt;country&gt;USA&lt;/country&gt; &lt;company&gt;Columbia&lt;/company&gt; &lt;price&gt;10.90&lt;/price&gt; &lt;year&gt;1985&lt;/year&gt; &lt;/cd&gt; &lt;cd&gt; &lt;title&gt;Hide your heart&lt;/title&gt; &lt;artist&gt;Bonnie Tyler&lt;/artist&gt; &lt;country&gt;UK&lt;/country&gt; &lt;company&gt;CBS Records&lt;/company&gt; &lt;price&gt;9.90&lt;/price&gt; &lt;year&gt;1988&lt;/year&gt; &lt;/cd&gt; &lt;cd&gt; &lt;title&gt;Greatest Hits&lt;/title&gt; &lt;artist&gt;Dolly Parton&lt;/artist&gt; &lt;country&gt;USA&lt;/country&gt; &lt;company&gt;RCA&lt;/company&gt; &lt;price&gt;9.90&lt;/price&gt; &lt;year&gt;1982&lt;/year&gt; &lt;/cd&gt; &lt;/catalog&gt; </code></pre> <p>"Empire Burlesque" should be the only item to pass the test, but all three titles pass the if statement and are outputted.</p> <p><strong>EDIT</strong></p> <p>Modifying the solution below, I assume this should work if I wanted to do the search and replace on a individual node basis? I won't be able to test it in Sharepoint until tomorrow.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:for-each select="catalog/cd"&gt; &lt;xsl:variable name="title_clean"&gt; &lt;xsl:call-template name="repNL"&gt; &lt;xsl:with-param name="pText" select="title"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:variable&gt; &lt;p&gt;&lt;xsl:value-of select='$title_clean' /&gt;&lt;/p&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;xsl:template name="repNL"&gt; &lt;xsl:param name="pText" select="."/&gt; &lt;xsl:copy-of select="substring-before(concat($pText,'&amp;#xA;'),'&amp;#xA;')"/&gt; &lt;xsl:if test="contains($pText, '&amp;#xA;')"&gt; &lt;br /&gt; &lt;xsl:call-template name="repNL"&gt; &lt;xsl:with-param name="pText" select= "substring-after($pText, '&amp;#xA;')"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload