Re: [xsl] replace &lt; and &gt; with < and >

Subject: Re: [xsl] replace &lt; and &gt; with < and >
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 19 Sep 2002 19:44:11 -0600 (MDT)
Ming Yu wrote:
> The xml file is returned from a method is in a string format, and the xml 
> file seems to encode all < and > using &lt; and &gt;. So, what I need to do 
> is to convert that xml file from a string to a xml nodelist so I can access 
> all the elements.
> Then, first I need to replace all &lt; and &gt; to < and > and then use a 
> method I wrote called parseXml which takes a string as parameter and 
> convert it to a nodelist.
> But I'm stuck on the first step which is converting all &lt; and &gt; to < 
> and >.
> Here is a sample of my xml file in a STRING:
> &lt;record&gt;
> &lt;items&gt;
> &lt;/items&gt;
> &lt;/record&gt;

I am guessing that despite what you see in the string, your XPath string
object actually does contain "<record>", and this only appears as
"&lt;record&gt;" when you copy and serialize it using the XML or HTML output

If so, then you only need to register your parseXml method as an extension
function, using the mechanism provided by your XSLT processor vendor, then
pass the string to it as-s.

If my guess is wrong, then you can use a recursive function to replace the
substrings. See for the
SubstringReplace template (this is also in the FAQ but the FAQ site seems to
be down today).

To replace &lt;, &gt;, and &amp; you'll have to call it 3x like this:

<xsl:variable name="betterXml">
  <xsl:call-template name="SubstringReplace">
    <xsl:with-param name="stringIn">
      <xsl:call-template name="SubstringReplace">
        <xsl:with-param name="stringIn">
          <xsl:call-template name="SubstringReplace">
            <xsl:with-param name="stringIn">
              <xsl:call-template name="SubstringReplace">
                <xsl:with-param name="stringIn" select="$theXml"/>
                <xsl:with-param name="substringIn" select="'&amp;lt;'"/>
                <xsl:with-param name="substringOut" select="'&lt;'"/>
            <xsl:with-param name="substringIn" select="'&amp;gt;'"/>
            <xsl:with-param name="substringOut" select="'&gt;'"/>
        <xsl:with-param name="substringIn" select="'&amp;amp;'"/>
        <xsl:with-param name="substringOut" select="'&amp;'"/>

Then pass string($betterXml) to your extension function.

   - Mike
  mike j. brown                   |  xml/xslt:
  denver/boulder, colorado, usa   |  resume:

 XSL-List info and archive:

Current Thread