Solved

Remove 1 in string from last in xslt

Posted on 2011-09-11
9
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

624 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