Unreplacing Entities

I

ion

Hi!
I'm trying to do the simplest thing in the world, that is
--> replace all [&"<>'] characters with entities
I saw the clever use of a string-replace template at
http://www.dpawson.co.uk/xsl/sect4/N10301.html, but I was hoping there
was some built-in function that I didn't know about. Is there some
simple way I can convert
"Don't forget that 1 > 0 & 1 < 2"
to
&quot;Don&apos;t forget that 1 &gt; 0 &amp; 1 &lt; 2&quot
? It just seems like a need that everyone would have.
Ion
 
R

Richard Tobin

ion said:
I'm trying to do the simplest thing in the world, that is
--> replace all [&"<>'] characters with entities
I saw the clever use of a string-replace template at
http://www.dpawson.co.uk/xsl/sect4/N10301.html, but I was hoping there
was some built-in function that I didn't know about.

Built in to what? Where are these strings that need escaping?

If they're in something that's supposed to be an XML file, then you
can't use an XML tool, because the XML parser won't be able to read a
file that has "1 < 2" in it. In that case you would need to use
some non-XML tool to put it right first, but how is it going to
distinguish between "real" angle brackets and ones that need to
be escaped?

You need to tell us more about the context.

-- Richard
 
I

ion

Richard,
Yes, you're right, I see that now. XSLT. I was hoping for a function
built in to XSLT.
Ion
 
I

ion

Richard,
OK. This is essentially what I want. I won't, as you point out, run
across < or > characters, and I don't have a problem with ampersands,
so I really just need to replace the quotes. Which I can essentially do
with the below template. I was just hoping for a simpler way. Thanks
for your help.
Ion

<!-- replace quotes in string with entities
Call with
<xsl:calltemplate name="quote-replace"><with-param name="arrgh"
select="."/></xsl:call-template>
-->
<xsl:template name="quote-replace">
<xsl:param name="arrgh"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="from" select="'"/>
<xsl:with-param name="to" select="&apos;'"/>
<xsl:with-param name="string">
<xsl:call-template name="string-replace">
<xsl:with-param name="from" select='"'/>
<xsl:with-param name="to" select="&quot;'"/>
<xsl:with-param name="string" select="$arrgh"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- template string-replace Copyright 1999 David Carlisle NAG Ltd
(e-mail address removed)
Free use granted under GPL or MPL.-->
<!-- replace all occurences of the character(s) `from' by the string
`to' in the string `string'.-->
<xsl:template name="string-replace">
<xsl:param name="string"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($string,$from)">
<xsl:value-of select="substring-before($string,$from)"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="string"
select="substring-after($string,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$string"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
 
R

Richard Tobin

ion said:
<xsl:call-template name="string-replace">
<xsl:with-param name="from" select="'"/>
<xsl:with-param name="to" select="&apos;'"/>
<xsl:with-param name="string">

Did you really mean that? You have &apos; followed by ' in $to.
Similarly in the second case you habe &quot; followed by '.

If you meant select="&apos;" that is equivalent to select="'", and
it won't change the string at all!

The XSLT processor will output single quotes as &apos; when necessary
anyway (and the only necessary place is in an attribute if it is
output in single quotes, which you can't control), so I'm afraid I
still don't understand your problem.

If you want to have something which is displayed in a browser as
"&apos;", you will need to have &amp;apos; in your XML.

-- Richard
 
I

ion

Richard,
OK, when I went to apply this, it turned out that the entity
substitution had already been done, and actually the whole procedure
was irrelevant. Thanks for your attempts to help; I apologize for
wasting your time.
Ion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top