Translating characters and enties

D

Don Robertson

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
 
P

Patrick TJ McPhee

% 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>
 
D

Dimitre Novatchev

Don Robertson said:
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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top