473,386 Members | 1,860 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,386 software developers and data experts.

XSLT: truncate last character?

I'm relatively new to XSLT, having strong background in bunch of other
programming languages (Perl, Java, etc.).

I'm writing an XSLT stylesheet, converting XML into HTML. Everything
works just fine, except for one detail which I don't know how to
handle. I have one element, which sometimes ends with a period, and
sometimes not (the value of that element that is). In the output
however, I need a period always. If I place a period inside my XSLT,
sometimes I end up with double periods.

Here's an XML example:

<stuff>
<element>blah 1</element>
<element>blah 2.</element>
<element>blah 3. another period. blah4</element>
<element>blah 5</element>
</stuff>

inside my XSLT there is this template:

<xsl:template match="element>
<xsl:apply-templates />. <!--note the ending period -->
<!-- or: <xsl:value-of select"." />. -->
</xsl:template>

This works in general (prints out the value), except for the occasional
double ending period.

The "element" tag can occasionally include a child tag, so the
apply-templates solution is preferable. My question is: is there a way
to remove the last character if it's a period?

I tried to use translate() function, but it removes all periods. I can
actually check for last character using these statements inside the
above template (btw. tried to nest functions, but it didn't work, so
used separate variable statements):

<xsl:variable name="element"><xsl:value-of select="." /></xsl:variable>
<xsl:variable name="element_len"><xsl:value-of
select="string-length($element)" ></xsl:variable>
<xsl:variable name="element_last"><xsl:value-of
select="substring($element, $element_len)" /></xsl:variable>

now, $element_last has the last character, and I can check if it's a
period or not. But that's all I can do. From what I know, there's no
mathematical functions I could use, to decrease $element_len, f.ex:

<xsl:value-of select="substring($element, $element_len-1)" />

If the above worked, that would be all I needed, but it just errs out.
Any advice?

Mar 30 '06 #1
2 15337
Use:

<xsl:value-of select="$theString"/>
<xsl:if test="not(substring($theString, string-length($theString)) = '.')
">
<xsl:text>.</xsl:text>
</xsl:if>

Cheers,
Dimitre Novatchev

"Sharkie" <sh******@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
I'm relatively new to XSLT, having strong background in bunch of other
programming languages (Perl, Java, etc.).

I'm writing an XSLT stylesheet, converting XML into HTML. Everything
works just fine, except for one detail which I don't know how to
handle. I have one element, which sometimes ends with a period, and
sometimes not (the value of that element that is). In the output
however, I need a period always. If I place a period inside my XSLT,
sometimes I end up with double periods.

Here's an XML example:

<stuff>
<element>blah 1</element>
<element>blah 2.</element>
<element>blah 3. another period. blah4</element>
<element>blah 5</element>
</stuff>

inside my XSLT there is this template:

<xsl:template match="element>
<xsl:apply-templates />. <!--note the ending period -->
<!-- or: <xsl:value-of select"." />. -->
</xsl:template>

This works in general (prints out the value), except for the occasional
double ending period.

The "element" tag can occasionally include a child tag, so the
apply-templates solution is preferable. My question is: is there a way
to remove the last character if it's a period?

I tried to use translate() function, but it removes all periods. I can
actually check for last character using these statements inside the
above template (btw. tried to nest functions, but it didn't work, so
used separate variable statements):

<xsl:variable name="element"><xsl:value-of select="." /></xsl:variable>
<xsl:variable name="element_len"><xsl:value-of
select="string-length($element)" ></xsl:variable>
<xsl:variable name="element_last"><xsl:value-of
select="substring($element, $element_len)" /></xsl:variable>

now, $element_last has the last character, and I can check if it's a
period or not. But that's all I can do. From what I know, there's no
mathematical functions I could use, to decrease $element_len, f.ex:

<xsl:value-of select="substring($element, $element_len-1)" />

If the above worked, that would be all I needed, but it just errs out.
Any advice?

Mar 31 '06 #2
Thanks a lot! This works great. I had to tweak it slightly, but the
concept works. Thanks again.

Mar 31 '06 #3

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

Similar topics

6
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last...
2
by: ad | last post by:
I have a string variable lik: sList="1.Tiger, 2.Wolf, 3.Rabbit," I want to trim off the last character "," Have there pre-define function to do that?
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
8
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why?...
4
by: Rasputin | last post by:
Hello, The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me...
3
by: rsennat | last post by:
hi, how do I remove the last character in a string only if its a "/". otherwise no need to remove the last character. im using std::string. tried with find_last_of( ). thanks rsennat
5
by: dudeja.rajat | last post by:
Sorry : Earlier mail had a typo in Subject line which might look in-appropriate to my friends Hi, I've a list some of whose elements with character \. I want to delete this last character...
0
by: Fredrik Lundh | last post by:
dudeja.rajat@gmail.com wrote: explicitly comparing against true is bad style; better write that as if item.endswith('\\'): item.endswith("\\") works just fine:
3
by: satyen46113 | last post by:
Hi Can any one help me,I want to store the last character typed in a textbox in a variable,and next time when the user enters another character ,the last chracter gets added to the previous...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.