BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
How to work with more than one xml document in your XSLT transformation
Sorting (advanced) of dynamically constructed strings
In case you need a ""break"" from an xsl:for-each loop
Format XML/XSL for Documentation
Find all xsl:variables that are in scope?
Binary Search in XSLT
Get the newest 5 articles out of hundreds -- a super efficient sort in XSLT
Dynamically change the encoding of your XSLT stylesheet
Identifying an attribute or a namespace node
Build a XPath expression for a node
Avoiding an XSLT Processor crash due to deep recursive processing
Conditional generation of a string in a single XPath expression
Re: Re: ABS function implemented as a single XPath expression
Re: ABS function implemented as a single XPath expression
ABS function implemented as a single XPath expression
XML to HTML text editor
Dynamically Selecting Which Element To Sort On Using Parameters
Avoid NaN in XPath sum() function
JScript Super Class To Handle XML Transformations
Test if a bit is on


 
 

<< XQuery.NET and XML >>


By Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First Posted 02/23/2001
Times viewed 719

How to do a search and replace in XSLT


This post contains attachments
v20010223112713.zip 

Summary This example demonstrates how to replace carriage returns with a break
in XSLT. It is also a generic template for handling any search and replace.

This example explains how to replace a carriage return with a break (<br />) in XSLT, so that the carriage return is preserved in a paragraph (<p>) in the HTML output.

In my XML example I have the following text in a CDATA section:

<blurb>
  <info>
<![CDATA[This displays many rows that we want to output.

The carriage return needs to be replaced in this example.]]>
  </info>
</blurb>

If I want to output this information in a <p> element (as I don't want to use a <pre>), the output of my example will be:

This displays many rows that we want to output. The carriage return needs to be replaced in this example.

All my carriage returns are lost!

When I first tackled this problem I was looking at the XPath translate() function solution.  But because the special character for a carriage return is &#10;, I actually need to do a replace of a word and not just a character.

So this example shows how you can replace the &#10; with a <br />.  This is an example of recursions where the template 'normalize-text' is called over and over inside of itself as it iterates through each word in the para.  If a carriage return is found, does the <br /> replace.

To achieve this replace I've used:

<xsl:value-of select=substring-before($text, $replace)/>
<xsl:value-of select=$by disable-output-escaping=yes/>

The attribute 'disable-output-escaping' tells the XSL processor to not escape any special characters, but to output exactly what is in the text node.

Additional information


Rate this article on a scale of 1 to 10 (1 votes, average 5)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML