We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Remove trailing period ... please help!!!!

jmc430
jmc430 asked
on
Medium Priority
644 Views
Last Modified: 2006-11-18
Greetings,

I am attempting to remove the period at the end of a text element string, like this:

  <SUB.HEAD>Here is a nice sub.headline.</SUB.HEAD>

I want it to read:  "Here is a nice sub.headline"  (without the final period)

However, the function I wrote currently removes all periods:

<xsl:value-of select="translate(.,'.','&#160;')"/>

So instead of the correct format as above, it appears like this:

"Here is a nice subheadline"

How can I simply remove the trailing/last character when it is a period?

Any advice or guidance is greatly appreciated.  Thanks for helping me!

Best regards,
Jamie
Comment
Watch Question

Commented:
A couple ways I can think of.. http://www.w3schools.com/xpath/xpath_functions.asp
<xsl:choose>
 <xsl:when test ="ends-with(.,'.')">
   <xsl:value-of select="substring(.,1,string-length(.)-1)"/><xsl:text>&#160;</xsl:text>
 </xsl:when>
 <xsl:otherwise>
   <xsl:value-of select="."/>
 </xsl:otherwise>
</xsl:choose>

If this happens a lot:
<xsl:value-of select="substring(.,1,string-length(.)-1)"/>
<xsl:value-of select="translate(substring(.,string-length(.)),'.','&#160;')"/>

or possibly:
<xsl:value-of select="substring(.,1,string-length(.)-1)"/>
<xsl:if test="not (ends-with(.,'.'))">
  <xsl:value-of select="substring(.,string-length(.))"/>
</xsl:if>

Author

Commented:
Hi jkmyoung,

Thanks for responding!

The ends-with function is not available within the constraints of my production environment .... and the trailing period does not occur all the time... is there any other way to work around this?

Thanks so much for helping me!

Author

Commented:
I've also been tinkering around with this:

<xsl:choose>
          <xsl:when test="substring(., string-length(.),1)='.'">
            <xsl:value-of select="substring(., 1,string-length(.) - 1)"/>
           </xsl:when>
          <xsl:otherwise>
             <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>

but it's not doing anything ... I have no idea what is happening!

Commented:
Did the 2nd suggestion:
<xsl:value-of select="substring(.,1,string-length(.)-1)"/>
<xsl:value-of select="translate(substring(.,string-length(.)),'.','&#160;')"/>
not work either?

eg. is the substring function available?

Author

Commented:
The substring function is available, but the 2nd suggestion did not work either ...

Author

Commented:
can you help me with this?

 <xsl:if test="string-length(translate(substring(.,string-length(.)),'.',''))!= 0">
   <xsl:apply-templates/>
 </xsl:if>
 <xsl:if test="string-length(translate(substring(.,string-length(.)),'.',''))= 0">
<xsl:value-of select="translate(.,'.','&#160;')"/>
 </xsl:if>

I am trying to do something similar that is also not working ...

Commented:
how did it not work? What is that outputting?

may need your surrounding xslt code.

Author

Commented:
Basically it's like this:

<SUB.HEAD>Here is a nice sub.headline.</SUB.HEAD>   => Should print           "Here is a nice sub.headline"

<SUB.HEAD>I. SUBHEAD.</SUB.HEAD>                         =>  Should print           "I. SUBHEAD"

<SUB.HEAD>II. "SUB HEAD."</SUB.HEAD>                     => Should print             "II. "SUB HEAD."
 
<SUB.HEAD>new subheadline.</SUB.HEAD>                  => Should print             "new subheadline"

Instead, all of my tests to just remove the trailing period are failing.  Either all the periods get removed, or all the periods end up staying.  I just want the final period to be removed - this is not happening often within the XML, but it is occurring often enough to cause grief amongst the Editors...

Thanks for helping me!
Commented:
<xsl:call-template name="trimPeriod">
  <xsl:with param name="str" select="."/>
</xsl:call-template>

<xsl:template name="trimPeriod">
  <xsl:param name="str" select="."/>
  <xsl:value-of select="substring-before($str,'.')"/>
  <xsl:variable name="rem" select="substring-after($str,'.')"/>
  <xsl:if test="$rem != ''">
    <xsl:value-of select="'.'"/>
    <xsl:call-template name="trimPeriod"/>
      <xsl:with-param name="str" select="$rem"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Is more the xslt that may be a concern. Perhaps it is not matching to the right templates?

also could try <xsl:if test="normalize-space($rem) != ''"> if perhaps there are spaces after the period.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
i realize that the problem has to do with the fact that whitespace is existing and messing up my tests for ending with a period.

 <xsl:if test="string-length(translate(substring(.,string-length(.)),'.',''))!= 0">
   <xsl:apply-templates/>
 </xsl:if>

 <xsl:if test="string-length(translate(substring(.,string-length(.)),'.',''))= 0">
<xsl:value-of select="translate(substring(.,string-length(.)),'.','&#160;')"/>
 </xsl:if>

how do i fix this so that it first normalize(s)-space (removes all trailing whitespace) before doing the test?

thanks so much for helping me..
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Hi,

ends-with() is an XSLT2.0 feature

here is how you can solve your issue

with this test XML
<?xml version="1.0" encoding="UTF-8"?>
<test>some text with.     </test>

this XSLT does the job,
you can reuse the named template

I saved the normalised string as a parameter,
that saves you a number of recalculations

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:call-template name="removeTrailingDot">
            <xsl:with-param name="argument" select="test"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="removeTrailingDot">
        <xsl:param name="argument"/>
        <xsl:param name="normArg" select="normalize-space($argument)"/>
        <xsl:choose>
            <xsl:when test="substring($normArg, string-length($normArg), 1) = '.'">
                <xsl:value-of select="substring($normArg, 1, string-length($normArg) - 1)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$normArg"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

I have a feeling it makes more sense testing the final character to be a dot
then calling the translate function
the substring method will be faster

cheers

Geert

Author

Commented:
thanks so much Geert!  you're awesome!  it works!! :)
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
welcome
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.