473,756 Members | 9,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;','&line break;')

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 2321
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:rep lace(., '&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="replacesu bstring.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="replaceme nt">
<xsl:text>\n</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:template>

<?xml version="1.0"?>
<!-- replace function by P McPhee -->
<xsl:styleshe et 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="replaceme nt" 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($origina l, $substring), $replacement)"/>
</xsl:with-param>

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

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

<xsl:with-param name="replaceme nt">
<xsl:value-of select="$replac ement"/>
</xsl:with-param>

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

--

Patrick TJ McPhee
East York Canada
pt**@interlog.c om
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;','&line break;')

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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:testmap=" testmap"
exclude-result-prefixes="testm ap"

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

<xsl:output omit-xml-declaration="ye s" indent="yes"/>

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

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

<xsl:choose>
<xsl:when test="$arg1 = '&#xA;'">\n</xsl:when>
<xsl:otherwis e>
<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\nL ine3\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
1560
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. in the first line I would appreciate it deeply ... for ($y = 0, $x = $cx-$cy-1; $y <= $cy; ++$y,++$x) { $prd = $q * $y_ar + $car; $prd -= ($car = intval($prd / 1E7)) * 1E7;
2
1447
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 -- re.sub('\\\\(.)', '\\\1', s) -- doesn't work, of course. The best I've come up with so far is a special case for every character to be translated. There must be an easier way? -- Henrik S. Hansen http://freecode.dk/~hsh/
1
2044
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, but I am stumped at translating LaTeX character encodings to something XML won't choke on. I found GNU recode to solve most of this, using cat milestone.tex | recode -d tex..xml | itemdb -s xml -o milestone.xml
8
2734
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 the time editing it with all the html code ie "ścią". Certainly someone must have a program that can do this automatically.
23
3153
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, won't translate it. When I paste the text into the code, the special characters are missing. If they would show up there I could use the Replace Special Characters feature to change it to the proper code, but it won't even paste into it...
0
1121
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 the contains statement syntax ' "DirectX" AND "managed" AND "code" '? I started writing a query parsers as an intermediary to handle control characters like , but frankly it's becoming a rather pain in the a$$. If anyone has any suggestions as...
1
1867
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 some help on translating it into SQL as a subquery so I can use it in an application. the queries in Access are the subquery... (query4) SELECT Movement.DateTime, Movement.StudentID, Movement.Subject, Movement.Teacher, Movement.Destination,...
9
1945
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 first 3 lines of a typical file. I understand everything except how the "a*23" at the end of the line is arrived at.
15
5885
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 do I translate the second line in a similiar compact way to python?
0
9482
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9292
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9901
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8733
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.