Lil Josh

Replace Function Workaround in XSLT 1.0

December 20, 2012

I came across a problem where I wanted to display HTML code on a page using XSLT, but I needed to replace all ‘<' and '>‘ characters with the HTML entity ‘<‘ and ‘>’. With XSLT 2.0 this would have been super simple using the built-in replace() function. However, I was stuck with XSLT 1.0. My solution was to use jQuery to select the element I wanted to display and then replace all the mentioned characters within it. Here’s an example:

$(document).ready(function() {
var body = $("body").html();
body = body.replace(/</g, "&lt;");
body = body.replace(/>/g, "&gt;");
$("body").html(body);
});
1.0 2.0 jquery replace XSLT