[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1734
  • Last Modified:

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

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>
0
headexplodes
Asked:
headexplodes
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
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
0
 
Geert BormansCommented:
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>
0
 
headexplodesAuthor Commented:
thanks for the reply! I will test this when i'm at work next and will most probably accept your answer!
0
 
headexplodesAuthor 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!
0
 
Geert BormansCommented:
welcome
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now