468,321 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

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 14249
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Daniel Mark | last post: by
5 posts views Thread by dudeja.rajat | last post: by
reply views Thread by Fredrik Lundh | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.