473,836 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_l en"><xsl:valu e-of
select="string-length($element )" ></xsl:variable>
<xsl:variable name="element_l ast"><xsl:valu e-of
select="substri ng($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="substri ng($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 15521
Use:

<xsl:value-of select="$theStr ing"/>
<xsl:if test="not(subst ring($theString , string-length($theStri ng)) = '.')
">
<xsl:text>.</xsl:text>
</xsl:if>

Cheers,
Dimitre Novatchev

"Sharkie" <sh******@yahoo .com> wrote in message
news:11******** *************@e 56g2000cwe.goog legroups.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_l en"><xsl:valu e-of
select="string-length($element )" ></xsl:variable>
<xsl:variable name="element_l ast"><xsl:valu e-of
select="substri ng($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="substri ng($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
2276
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 character? I am hoping the DSO will not have to read thru the whole file. it is probably way to big. Thank youi very much
2
45276
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
18421
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
80316
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? Thanks
4
7410
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 know for next time. I am reading strings from a file to a vector, doing some "operations" on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last...
3
31805
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
15602
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 from the elements that have this character set at their end,
0
1148
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
2002
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 character. Plz reply soon
0
9668
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
10546
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
10588
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
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7790
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
6978
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
5647
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
4448
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
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.