473,387 Members | 1,757 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.

Stripping whitespace around certain elements

I'm having problems stripping out the whitespace at the beginning of a
particular element. In the XML snippet I've highlighted tabs and returns
as ^I and ^M respectively:

<para> ^I ^I ^I^M
Some text with occasional highlighting. Some text with occasional^M
highlighting. Some text with occasional <high>highlighting</high>.^M
Some text with occasional highlighting. Some <high>text</high> with^M
occasional highlighting.</para>^M

I'd like my XSL to remove all whitespace immediately following <para> and
convert all other ^M's to a single space. My attempt:

<xsl:template match="para">{P}<xsl:apply-templates/>{\P}</xsl:template>
<xsl:template match="high">{B}<xsl:apply-templates/>{\B}</xsl:template>
<xsl:template match="text()"><xsl:value-of select="normalize-space()"/>
</xsl:template>

Strips out too much white space:

{P}Some text with occasional highlighting. Some text with occasional
highlighting. Some text with occasional{B}highlighting{\B}. Some
text with occasional highlighting. Some{B}text{\B}with occasional
highlighting.{\P}

Where the spaces are lost around the highlighting. I understand why it's
happening but can't fix it. Adding spaces around {B} and {\B} in the XSL
helps to some extent, but breaks if the highlighting is at the beginning
or end of <para> and around punctuation.

Could somebody help me make it more selective?

Many thanks,
Andy.
Jul 20 '05 #1
1 2466
"Colin Mackenzie" <co***@elecmc.com> wrote :
Not sure if this is what you want to do, but it sort of works.
Thanks! There's just one case I've found where this doesn't work but it's
perfectly acceptable until I can invest the time in learning extensions.
Text processing (as opposed to node processing) is still best done in any
language with regular expresion handling built0-in or via extensions (Perl,
Python, Java, VB, VBScript, JScript etc).
Yes...

Cheers!
Andy.

This would be easier with XSLT2 I guess...

<xsl:template match="para">{P}<xsl:apply-templates/>{\P}</xsl:template>
<xsl:template match="high">{B}<xsl:apply-templates/>{\B}</xsl:template>

<xsl:template match="text()">
<!-- if its NOT the first node in the parent and it starts with a space,
out put space -->
<xsl:if test="(position() != 1) and (substring(., 1,1)=' ')">
<xsl:text> </xsl:text>
</xsl:if>
<!-- throw away all leading or trailing spaces -->
<xsl:value-of select="normalize-space()"/>

<!-- if it ends with a space, bung a space out -->
<xsl:if test="substring(., string-length(.),1)=' '">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>

Jul 20 '05 #2

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

Similar topics

4
by: joram gemma | last post by:
Hello, on windows python 2.4.1 I have the following problem >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona' >>> print s D:\music\D\Daniel Lanois\For the beauty of Wynona >>>...
0
by: Curtiss Howard | last post by:
I'm using Xerces 2.4.0 and I've got a schema (converted from a DTD) for my XML documents. However, Xerces will NOT ignore the whitespace between elements. These are the options I've set for the...
2
by: josh.asbury | last post by:
I am having some major issues with whitespace in my XSL stylesheets. We recently upgraded our servlet-based application to JDK 1.4, and this has forced the issue of my finally upgrading to...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
6
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
6
by: Medros | last post by:
I understand that you can strip html out of a txt file so that all the information is left is the visable information that is needed (e.g. everything that has < > around is gone). My question is...
1
by: Foxpointe | last post by:
Given some arbitrary XHTML, I'd like to obtain a 'simplified' XHTML result which strips out a large subset of standard elements and attributes - but not all. The main things I would like to...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
14
by: ryan k | last post by:
Hello. I have a string like 'LNAME PASTA ZONE'. I want to create a list of those words and basically replace all the whitespace between them with one space so i could just do...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...

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.