Translating characters and enties

Discussion in 'XML' started by Don Robertson, Jan 5, 2004.

  1. Greetings,

    I need to be able to output text with some characters escaped - eg change a

    to \n

    I am at a loss as how to do this. I am trying

    translate($text,'
    ','\n')

    and

    <!DOCTYPE stylesheet [
    <!ENTITY linebreak
    "\n">
    ]>

    translate($text,'
    ','&linebreak;')

    but all I am getting is the '\'

    I have just realised - would I have to do a recursive contains
    substring-before substring-after loop? it is to late to try now ... but if
    anyone has a suggestion on how to do this Id appreciate it

    Don
    Don Robertson, Jan 5, 2004
    #1
    1. Advertising

  2. In article <>,
    Don Robertson <> wrote:

    % I am at a loss as how to do this. I am trying
    %
    % translate($text,'
    ','\n')

    translate() works on single characters. This will change each new-line
    to a back-slash, and do nothing with the n.

    You want a `replace' function, but there's none provided in xpath.
    When faced with this situation, it's often helpful to visit
    http://www.exslt.org and see if there's something there which does
    what you want (many XSLT processors support the exslt extensions).

    In this case, there's str:replace, so you could have
    <xsl:template match='text()' xmlns:str='http://exslt.org/strings'>
    <xsl:value-of select='str:replace(., '&xA;', '\n')
    </xsl:template>

    If that doesn't work, it's possible to create a template which performs
    replace operations. I've appended mine below. You use it like this:

    <!-- xsl:import must go at the top of the stylesheet -->
    <xsl:import href="replacesubstring.xsl"/>

    <xsl:template match='text'()'>
    <xsl:call-template name='replace-substring'>
    <xsl:with-param name="original">
    <xsl:value-of select="string(.)"/>
    </xsl:with-param>
    <xsl:with-param name="substring">
    <xsl:text>
    </xsl:text>
    </xsl:with-param>
    <xsl:with-param name="replacement">
    <xsl:text>\n</xsl:text>
    </xsl:with-param>
    </xsl:call-template>
    </xsl:template>

    <?xml version="1.0"?>
    <!-- replace function by P McPhee -->
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template name="replace-substring">
    <xsl:param name="prefix"/>
    <xsl:param name="original"/>
    <xsl:param name="substring"/>
    <xsl:param name="replacement" select="''"/>

    <xsl:choose>
    <xsl:when test="contains($original, $substring)">
    <xsl:call-template name="replace-substring">

    <xsl:with-param name="prefix">
    <xsl:value-of select="concat($prefix, substring-before($original, $substring), $replacement)"/>
    </xsl:with-param>

    <xsl:with-param name="original">
    <xsl:value-of select="substring-after($original, $substring)"/>
    </xsl:with-param>

    <xsl:with-param name="substring">
    <xsl:value-of select="$substring"/>
    </xsl:with-param>

    <xsl:with-param name="replacement">
    <xsl:value-of select="$replacement"/>
    </xsl:with-param>

    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="concat($prefix, $original)"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>

    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Jan 5, 2004
    #2
    1. Advertising

  3. "Don Robertson" <> wrote in message
    news:p...
    > Greetings,
    >
    > I need to be able to output text with some characters escaped - eg change

    a
    >
    to \n
    >
    > I am at a loss as how to do this. I am trying
    >
    > translate($text,'
    ','\n')
    >
    > and
    >
    > <!DOCTYPE stylesheet [
    > <!ENTITY linebreak
    > "\n">
    > ]>
    >
    > translate($text,'
    ','&linebreak;')
    >
    > but all I am getting is the '\'
    >
    > I have just realised - would I have to do a recursive contains
    > substring-before substring-after loop? it is to late to try now ... but if
    > anyone has a suggestion on how to do this Id appreciate it



    Using FXSL this is very straightforward.

    This transformation:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:testmap="testmap"
    exclude-result-prefixes="testmap"
    >

    <xsl:import href="str-map.xsl"/>
    <xsl:eek:utput method="text"/>

    <xsl:eek:utput omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
    <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
    <xsl:call-template name="str-map">
    <xsl:with-param name="pFun" select="$vTestMap"/>
    <xsl:with-param name="pStr" select="/*"/>
    </xsl:call-template>
    </xsl:template>

    <testmap:testmap/>
    <xsl:template name="putNL" match="*[namespace-uri() = 'testmap']">
    <xsl:param name="arg1"/>

    <xsl:choose>
    <xsl:when test="$arg1 = '
    '">\n</xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$arg1"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>

    when applied on this source.xml:

    <t>
    This is a multiline text -- line1
    Line2
    Line3
    </t>

    Produces the wanted result:

    \nThis is a multiline text -- line1\nLine2\nLine3\n


    Hope this helped.


    Dimitre Novatchev.
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
    Dimitre Novatchev, Jan 5, 2004
    #3
    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. Curt_C [MVP]
    Replies:
    1
    Views:
    415
    Curt_C [MVP]
    Jul 7, 2004
  2. Grzegorz Wrazen

    JAI and Translating image

    Grzegorz Wrazen, Jul 15, 2004, in forum: Java
    Replies:
    0
    Views:
    373
    Grzegorz Wrazen
    Jul 15, 2004
  3. kimi
    Replies:
    2
    Views:
    2,042
    Roedy Green
    Feb 24, 2006
  4. Henrik S. Hansen

    Translating escaped characters

    Henrik S. Hansen, Nov 1, 2004, in forum: Python
    Replies:
    2
    Views:
    283
    Henrik S. Hansen
    Nov 2, 2004
  5. Ken Fine
    Replies:
    2
    Views:
    165
    Ken Fine
    Feb 24, 2004
Loading...

Share This Page