Link to home
Start Free TrialLog in
Avatar of jmc430
jmc430

asked on

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

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
Avatar of jkmyoung
jkmyoung

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>
Avatar of jmc430

ASKER

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!
Avatar of jmc430

ASKER

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!
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?
Avatar of jmc430

ASKER

The substring function is available, but the 2nd suggestion did not work either ...
Avatar of jmc430

ASKER

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 ...
how did it not work? What is that outputting?

may need your surrounding xslt code.
Avatar of jmc430

ASKER

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!
SOLUTION
Avatar of jkmyoung
jkmyoung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmc430

ASKER

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..
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmc430

ASKER

thanks so much Geert!  you're awesome!  it works!! :)