We help IT Professionals succeed at work.

Replacing '&gt;' with '<' using XSLT

headexplodes
headexplodes asked
on
Medium Priority
2,952 Views
Last Modified: 2012-06-27
Hi,

I am trying to format some fairly simple data into HTML using an XSLT stylesheet. One of the fields in the XML data actually contains HTML syntax, but every < and > is converted into an &gt; and &lt;. I have found examples of how to replace text using simple template with a couple of parameters. This works quite well for simple text replacements. However, I run into a problem when I try and replace an '&gt;' with a '>'.

The problem is that if I literally write '&gt;' as the replace string, it really does just put '&gt;' in the output. However, if I enter '>' an the replace string, it seems to get escaped somewhere along the way and comes out as '&gt;' in the output (not what I want).

So, my question is: How can I replace every '&gt;' in some XML data into a real '>' using an XSLT stylesheet?

Thanks in advance,
Robert.

---

Below is my current stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data/row">
      
      <xsl:variable name="gtsrc">&gt;</xsl:variable>
      <xsl:variable name="gtdest">></xsl:variable>
      
      <html>
            
            <h1><xsl:value-of select="$gtsrc"/></h1>
            
            <xsl:for-each select="field">
                  <p>
                        <xsl:call-template name="replace-string">
                              <xsl:with-param name="text" select="."/>
                              <xsl:with-param name="replace" select="$gtsrc"/>
                              <xsl:with-param name="with" select="$gtdest"/>
                        </xsl:call-template>
                  </p>
            </xsl:for-each>
            
      </html>
      
</xsl:template>
<xsl:template name="replace-string">
      
      <xsl:param name="text"/>
      <xsl:param name="replace"/>
      <xsl:param name="with"/>
      
      <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                  <xsl:value-of select="substring-before($text,$replace)"/>
                  <xsl:value-of select="$with"/>
                  <xsl:call-template name="replace-string">
                        <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                        <xsl:with-param name="replace" select="$replace"/>
                        <xsl:with-param name="with" select="$with"/>
                  </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                  <xsl:value-of select="$text"/>
            </xsl:otherwise>
      </xsl:choose>
      
</xsl:template>
</xsl:stylesheet>

Current data:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="printing-test.xsl"?>
<data>
      <row>
            <field>Some simple data</field>
            <field>Here is some &lt;strong&gt;HTML&lt;/strong&gt; data.</field>
      </row>
</data>
Comment
Watch Question

Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
you have to disable the output escaping

           <h1><xsl:value-of select="$gtsrc" disable-output-escaping="yes"/></h1>

don't use it to create non wellformed html though
having to create wellformed output can be a pain in the beginning
but you will soon get the habit right
and escaping all tags and disable the escaping after it is often bad design

the functionality exsists.... but use it with caution

cheers

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
here is your new stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="data/row">
        <xsl:variable name="gtsrc">&gt;</xsl:variable>
        <xsl:variable name="gtdest">></xsl:variable>
        <html>
            <h1><xsl:value-of select="$gtsrc" disable-output-escaping="yes"/></h1>
           <xsl:for-each select="field">
                <p><xsl:value-of select="." disable-output-escaping="yes"/></p>
            </xsl:for-each>
       </html>
    </xsl:template>
</xsl:stylesheet>

Author

Commented:
thanks for the reply! I will test this when i'm at work next and will most probably accept your answer!

Author

Commented:
Works well. One other thing that was screwing me up was that i was trying to use it with firefox, which seems to ignore the "disable-output-escaping" option. Using Xalan and internet explorer works fine. Damn u firefox! Now I hate both firefox and internet explorer :p.

Anyway, thanks for the help!
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
welcome
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.