Unreplacing Entities

Discussion in 'XML' started by ion, Jan 13, 2005.

  1. ion

    ion Guest

    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
     
    ion, Jan 13, 2005
    #1
    1. Advertising

  2. In article <>,
    ion <> wrote:

    >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
     
    Richard Tobin, Jan 13, 2005
    #2
    1. Advertising

  3. ion

    ion Guest

    Richard,
    Yes, you're right, I see that now. XSLT. I was hoping for a function
    built in to XSLT.
    Ion
     
    ion, Jan 13, 2005
    #3
  4. ion

    ion Guest

    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

    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>
     
    ion, Jan 13, 2005
    #4
  5. In article <>,
    ion <> wrote:

    ><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
     
    Richard Tobin, Jan 13, 2005
    #5
  6. ion

    ion Guest

    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
     
    ion, Jan 31, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. R Paley
    Replies:
    2
    Views:
    509
    Paul Uiterlinden
    Nov 20, 2004
  2. Replies:
    1
    Views:
    439
    Mike Treseler
    Jan 14, 2005
  3. Andy Peters

    instances of entities vs components

    Andy Peters, Jul 12, 2005, in forum: VHDL
    Replies:
    1
    Views:
    567
    Mike Treseler
    Jul 13, 2005
  4. Joergen Bech
    Replies:
    2
    Views:
    4,439
    Joergen Bech
    Jun 14, 2005
  5. Jim Higson
    Replies:
    3
    Views:
    224
    Eric Amick
    Jul 25, 2004
Loading...

Share This Page