Searching the newline charcter using XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amollokhande1
    New Member
    • Feb 2008
    • 33

    Searching the newline charcter using XSLT

    Hi,

    I am fetching the data from the database and assigning it to the textarea. Before that Xml is loaded with data and then stylesheet gets applied to the xml to generate the html output with textarea with data in it. I want to replace the newline character with '\n' character. I tried to use following psedudo code

    :
    :
    <xsl:when test="contains( $text,' ')">
    <xsl:value-of select="substri ng-before($text, ' ')"/>\n<xsl:call-template name="break">
    <xsl:with-param name="text" select="substri ng-after($text,' ' )"/>
    </xsl:call-template>
    </xsl:when>

    This did not worked. I have also tried to use '&#xa' instead but that also did not worked.

    Can anybody tell me how to search and replace the newline character using xslt?

    Regards
    Amol Lokhande
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you could try
    Code:
    <xsl:text>&#38;#10;</xsl:text>
    since \n doesn't have any special meaning is xslt.

    Comment

    • Sid Fadnis

      #3
      newline in xslt

      I think both should work

      Code:
      	<xsl:text>&#xD;</xsl:text>
      	<xsl:text>
      </xsl:text>

      ---------------------------------------------
      Sample XML FIle
      ---------------------------------------------
      Code:
      <?xml version="1.0"?>
      <root>
      <library id="1">
      <books>
      <book id="1">
      	<name>Book 1</name>
      	<author>Author A</author>
      </book>
      <book id="2">
      	<name>Book 2</name>
      	<author>Author B</author>
      </book>
      <book id="3">
      	<name>Book 3</name>
      	<author>Author C</author>
      </book>
      <book id="4">
      	<name>Book 4</name>
      	<author>Author A and Author D</author>
      </book>
      </books>
      </library>
      </root>
      ---------------------------------------------
      Sample XSL FIle
      ---------------------------------------------
      Code:
      <?xml version="1.0"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="text"/>
      <xsl:template match="/">
      	<xsl:text>&#xD;</xsl:text>
      	<xsl:apply-templates select="root/library[./@id = '1']/books/book"/>
      </xsl:template>
      <xsl:template match="book">
      	<xsl:value-of select="./@id"/>
      	<xsl:value-of select="' Title: '"/>
      	<xsl:value-of select="name"/>
      	<xsl:value-of select="' Author: '"/>
      	<xsl:value-of select="author"/>
      	<xsl:text>&#xD;</xsl:text>
      	<xsl:text>
      </xsl:text> 
      </xsl:template>
      </xsl:stylesheet>
      ---------------------------------------------
      Output (notice two new lines appended after each row)
      ---------------------------------------------
      1 Title: Book 1 Author: Author A


      2 Title: Book 2 Author: Author B


      3 Title: Book 3 Author: Author C


      4 Title: Book 4 Author: Author A and Author D
      Last edited by Dormilich; Sep 28 '10, 05:50 AM.

      Comment

      • Sid Fadnis

        #4
        further

        in my post above the browser converted this code

        <xsl:text> & # 1 0 ; </xsl:text>

        (remove all spaces from abobve)

        to

        <xsl:text> </xsl:text>

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Could try:
          Code:
          <xsl:variable name="newline">
          <xsl:text>
          </xsl:text>
          </xsl:variable>
          What is processing the result of the transformation?

          Comment

          Working...