Solved

Remove 1 in string from last in xslt

Posted on 2011-09-11
9
307 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36521012
no choose construct nor if construct required,
0
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
XSL-FO - show results in 2 columns 5 32
Cordova Camera plugin fails 2 140
.php tree directory? 5 83
Download a website to hdd 2 80
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

790 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