Solved

Remove 1 in string from last in xslt

Posted on 2011-09-11
9
301 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tool to email me when a website changes 29 120
VB.NET XML Processing a XMLNodeList with Namespace 16 67
Animated .jpg? 13 61
Randomize in Owl Carousel v1.3.2 6 20
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

867 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

20 Experts available now in Live!

Get 1:1 Help Now