Solved

Remove 1 in string from last in xslt

Posted on 2011-09-11
9
311 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
Independent Software Vendors: 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!

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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