473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Translating characters and enties

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,'&#xA;','&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
Jul 20 '05 #1
2 2302
In article <pa****************************@robertson.net.nz >,
Don Robertson <do*@robertson.net.nz> wrote:

% I am at a loss as how to do this. I am trying
%
% translate($text,'&#xA;','\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>&#xA;</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:otherwise>
<xsl:value-of select="concat($prefix, $original)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2

"Don Robertson" <do*@robertson.net.nz> wrote in message
news:pa****************************@robertson.net. nz...
Greetings,

I need to be able to output text with some characters escaped - eg change a &#xA; to \n

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

translate($text,'&#xA;','\n')

and

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

translate($text,'&#xA;','&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:output method="text"/>

<xsl:output 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 = '&#xA;'">\n</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$arg1"/>
</xsl:otherwise>
</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

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Davis Marques | last post by:
hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc....
2
by: Henrik S. Hansen | last post by:
How do you best go about translating characters like '\\n' to '\n'? This is for a configuration file parser, where the "backslash convention" is supported. The naive approach --...
1
by: Michael Friendly | last post by:
I have a LaTeX document describing a long list of items that I want to translate to XML to treat these as a database. I've written a perl script to do the basic translation, and a basic DTD file,...
8
by: gregf | last post by:
Is there a way or a program (for windows) that can translate foreign characters inot the proper html code? I have a word document with many different characters and I really don't want to spend all...
23
by: gregf | last post by:
I have a paragraph of text pasted into a word document, it's in Polish, complete with polish characters. They show up just fine in word, but the program I use for web page programming, HomeSite,...
0
by: Dylan Phillips | last post by:
I'm interested in how other participants in this new group are implementing SQL Full-Text Search on their Web Sites. How are you translating the user search string: "DirectX managed code" into...
1
by: timn | last post by:
Translating Access SQL queries into SQL subqueries. -------------------------------------------------------------------------------- I have a query in Access that uses a subquery, I would like...
9
by: lombardm | last post by:
I am trying to decipher/translate some code that I have no experience with. I am trying to find out how the checksum is computed for a Megellan Explorist GPS Waypoint (POI) file. Below is the...
15
by: hofer | last post by:
Hi, Let's take following perl code snippet: %myhash=( one =1 , two =2 , three =3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # <-- line of interest print "$v1\n$v2\n$v2\n"; How...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.