Solved

Replace CRLF with <P> in XSL

Posted on 2001-06-14
6
1,347 Views
Last Modified: 2011-04-14
I have some XSL like this,

  <xsl:copy-of select="$datavalue" />

and datavalue contains carriage return/line feed characters.  Is it possible (in XSL) to replace the CRLF with a "<P/>" tag for correct rendering in HTML?

In Visual Basic, I would just do:

  mystr=Replace(mystr, chr$(13), "<P/>")

0
Comment
Question by:anthony_glenwright
6 Comments
 
LVL 3

Expert Comment

by:Minna
ID: 6194054
Try the translate(value, from, to) function
0
 
LVL 6

Author Comment

by:anthony_glenwright
ID: 6195699
I tried translate - it doesn't work the way you would think, since it replaces matching characters from the second string with those in the same position in the 3rd string...

i.e. translate ("AAA", "AZZ", "RRR") = "RAA"

strange that they would even make up such a strange function...

PS:  Are you the minna that I know?


0
 
LVL 15

Expert Comment

by:robbert
ID: 6196654
I do that by a server-side VBScript script. If there'll be no XSLT solution, I'll post it.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Accepted Solution

by:
MFlint earned 50 total points
ID: 6199866
Anthony,

Check out this page (which is where I learned this trick):
http://www.dpawson.co.uk/xsl/sect2/replace.html

Then try something like this:

     <xsl:template match="text()" priority="2">          <!-- Increase priority to avoid warning messages about duplicate templates -->
          <xsl:call-template name="break"/>
     </xsl:template>
     

     <xsl:template name="break">                         <!-- Converts any #xa characters into <br/> tags for display.  -->
          <xsl:param name="text" select="."/>
          <xsl:choose>
               <xsl:when test="contains($text, '&#xa;')">
                    <xsl:value-of select="substring-before($text, '&#xa;')"/>
                    <br/>
                    <xsl:call-template name="break">
                         <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
                    </xsl:call-template>
               </xsl:when>
               <xsl:otherwise>
                    <xsl:value-of select="$text"/>
               </xsl:otherwise>
          </xsl:choose>
     </xsl:template>

0
 

Expert Comment

by:MFlint
ID: 6199918
Minna and Anthony,

FYI about the xPath function 'translate()', check out this page:
http://www.w3.org/TR/xpath#function-translate

----------------------------------

The translate function returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. For example, translate("bar","abc","ABC") returns the string BAr. If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), then occurrences of that character in the first argument string are removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, then excess characters are ignored.

NOTE: The translate function is not a sufficient solution for case conversion in all languages. A future version of XPath may provide additional functions for case conversion.

----------------------------------

Mostly I've used it to convert from one case to another, so for example I'll have something like this at the very top of my XSL file:

<!DOCTYPE xsl:stylesheet [
     <!ENTITY indenthtml "no">
     <!-- NOTE: Change indenthtml indent to "yes" for debugging generated HTML and "no" for code optimized for production -->
     <!ENTITY nbsp "&#160;">
     <!ENTITY bgcolor1 "#FFFFFF">     <!-- white -->
     <!ENTITY bgcolor2 "#CC6600">     <!-- dark orange  -->
     <!ENTITY bgcolor3 "#FFCC66">     <!-- light orange -->
     <!ENTITY txtcolor1 "#000000">     <!-- black -->
     <!ENTITY compWidth "670">          <!-- Component width / width of table that holds the component -->
     <!ENTITY lowerCase "abcdefghijklmnopqrstuvwxyz">
     <!ENTITY upperCase "ABCDEFGHIJKLMNOPQRSTUVWXYZ">
]>


Then later in the document I could do something like:

               <td  width="16%" valign="top" class="MOIDocText">
                    <xsl:value-of select="translate(LIMITS/DETAIL[1]/TYPE, '&lowerCase;', '&upperCase;')"/>
                    <xsl:text>&nbsp;</xsl:text>
               </td>

0
 
LVL 6

Author Comment

by:anthony_glenwright
ID: 6205577
Thanks all - I ended up solving a different way (at the database end), but MFlint's answer is a solution...
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Nessus Scan 1 73
write xml in vb.net 2 34
XSLT 1.0 - Finding Substrings from the End of a String 3 32
How does proportional-column-width work in xsl fo 4 15
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question