Solved

Remove 1 in string from last in xslt

Posted on 2011-09-11
9
297 Views
Last Modified: 2013-11-18
Hi,

my xsl code
<xsl:value-of select="."/>

sometime it brings text with 1 in last.

I wanted to remove 1 printed in last

I mean if

value of

<xsl:value-of select="."/>

is

"24 Hour Room Service1"
"Air Conditioning1"

then it should print
"24 Hour Room Service"
"Air Conditioning"

without 1 in last

if value is without 1 then it should not do anything

I mean if value is

"Room Safe "
"Shopping "
"Spa"

then reult should print

"Room Safe "
"Shopping "
"Spa"


Please help me in removing 1 in last from below code



<xsl:for-each select="Facilities">
      <li>
            <xsl:value-of select="."/>
      </li>
</xsl:for-each>

Many Thanks
0
Comment
Question by:tia_kamakshi
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:sshah254
ID: 36520932
You'll need to do something like this (I don't remember the exact syntax since it has been a while that I have written in XSLT)

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

You get the idea ...

SS
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36521005
about 12 typos removed from the above suggestion

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

Expert Comment

by:Geert Bormans
ID: 36521010
But I would do the following.

Output all but the last character
do a translate on the last character (replace with '' when '1')

faster, cleaner ... and tested :-)
<li>
                <xsl:value-of select="substring(., 1, string-length(.) -1 )"/>
                <xsl:value-of select="translate(substring(., string-length(.)), '1', '')"/>
            </li>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36521012
no choose construct nor if construct required,
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:tia_kamakshi
ID: 36521032
Hi,

Thanks for your reply.

The solution provided is not working. I tried below code

<xsl:for-each select="Facilities">
                  <xsl:choose>
                    <xsl:when test='substring(. , string-length(.)-1) = 1'>

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




Please help me fixing the code

Many Thanks again
0
 

Author Comment

by:tia_kamakshi
ID: 36521066
Thanks Gertone, your below code works great

<li>
                <xsl:value-of select="substring(., 1, string-length(.) -1 )"/>
                <xsl:value-of select="translate(substring(., string-length(.)), '1', '')"/>
            </li>


Can you please help me understanding your above code

Many Thanks again
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 36521289
There are two value-of statements
The first outputs the entire string except the last character: substring ranging from the first character to the last but one character
The second does a special treatment on the last character: substring from the last character to the end, or effective the last character
translate does the following: it replaces a character in the first argument, that is found in the second argument with the character that is found at the same location in the third argument (if there is a character at that location)
basically it replaces every '1' with a '')... but sinces this only works on the last character, it will just drop the last character if it is a '1'
0
 

Author Closing Comment

by:tia_kamakshi
ID: 36527191
Many Many thanks for your help
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36527530
welcome
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now