?
Solved

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

Posted on 2006-03-24
13
Medium Priority
?
623 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
0
Comment
Question by:jmc430
  • 7
  • 4
  • 2
13 Comments
 
LVL 12

Expert Comment

by:jkmyoung
ID: 16284454
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
 

Author Comment

by:jmc430
ID: 16284548
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
 

Author Comment

by:jmc430
ID: 16284564
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:jkmyoung
ID: 16284710
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
 

Author Comment

by:jmc430
ID: 16284767
The substring function is available, but the 2nd suggestion did not work either ...
0
 

Author Comment

by:jmc430
ID: 16284883
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
 
LVL 12

Expert Comment

by:jkmyoung
ID: 16284888
how did it not work? What is that outputting?

may need your surrounding xslt code.
0
 

Author Comment

by:jmc430
ID: 16284918
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
 
LVL 12

Assisted Solution

by:jkmyoung
jkmyoung earned 600 total points
ID: 16285047
<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
 

Author Comment

by:jmc430
ID: 16285110
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1400 total points
ID: 16288228
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
 

Author Comment

by:jmc430
ID: 16315390
thanks so much Geert!  you're awesome!  it works!! :)
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16318256
welcome
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question