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
jmc430Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkmyoungCommented:
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>
0
jmc430Author 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!
0
jmc430Author 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!
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jkmyoungCommented:
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?
0
jmc430Author Commented:
The substring function is available, but the 2nd suggestion did not work either ...
0
jmc430Author 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 ...
0
jkmyoungCommented:
how did it not work? What is that outputting?

may need your surrounding xslt code.
0
jmc430Author 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!
0
jkmyoungCommented:
<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.
0
jmc430Author 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..
0
Geert BormansInformation ArchitectCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmc430Author Commented:
thanks so much Geert!  you're awesome!  it works!! :)
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.