Re: [xsl] Adding a break character between a group of input data except for the last entry

Subject: Re: [xsl] Adding a break character between a group of input data except for the last entry
From: "Steve Assad" <SAssad@xxxxxxxxxxx>
Date: Wed, 5 Dec 2001 17:23:17 -0800
Thanks to Peter Davis and Joerg
 for the help. It works great using  <xsl:if test="position() != last()">;
</xsl:if>
sorry it took so long to get back got sidetracked on another project.
I still don't understand why the original code did not work. I know your
solution is better but for my understanding.
I did the following

             <annotationsText>
             <xsl:variable name="line" select="prescriber-info/trade-name
[.] "/>
             <first_tradename><xsl:value-of select="$line"/></
first_tradename>
                 <xsl:for-each select="prescriber-info/trade-name">
                 <xsl:variable name="trade" select="."/>
                 <xsl:if test="$trade != $line">;</xsl:if>
                     <xsl:value-of select="$trade"/>
                 </xsl:for-each>
             </annotationsText>

IF there is only one trade-name in the list it works. For example trade
name 'run'  the output is
<annotationsText>
    <first_tradename>run</first_tradename>
    run
</annotationsText>

Note no ';' fine worked and the if statement worked.

but if there is a list of trade-names for example  'run',' jump',' high'
then the out put will be

<annotationsText>
    <first_tradename>run</first_tradename>
    ;run;jump;high
</annotationsText>

I am displaying the $trade and $line and they have the same value. For
multiple words it is not working.
Just for my understanding can some one please explain this.

Regards

Steve



                                                                                                                           
                    Peter Davis                                                                                            
                    <pdavis@xxxxxxxxx>                To:     xsl-list@xxxxxxxxxxxxxxxxxxxxxx                              
                    Sent by:                          cc:     (bcc: Steve Assad/Sentius)                                   
                    owner-xsl-list@xxxxxxxxxxxx       Subject:     Re: [xsl] Adding a break character between a group of   
                    rytech.com                         input data except for the last entry                                
                                                                                                                           
                                                                                                                           
                    11/29/01 04:45 PM                                                                                      
                    Please respond to xsl-list                                                                             
                                                                                                                           
                                                                                                                           




On Thursday 29 November 2001 04:19 pm, Steve Assad wrote:
>  <annotationsText>
>                <xsl:variable name="line" select
="prescriber-info/trade-name
> "/>
>                    <xsl:for-each select="prescriber-info/trade-name">
>                              <xsl:variable name="trade_names" select="
> prescriber-info/trade-name"/>
>                              <xsl:if test= "$trade_names != $line">; </
> xsl:if> <xsl:value-of select="."/></xsl:for-each>
>              </annotationsText>

Your method of testing the string won't work for a couple reasons.  First,
the two variables you are declaring, $line and $trade_names, are always
equal.  The select="prescriber-info/trade-name" is not affected by the fact

that it is inside a for-each loop.  To properly select the current value,
you
would want do do select="current()" or select=".".  But in addition to that

problem, the $line variable will select *all* of the trade-name elements,
while $trade_names would only be the current element, and so any "!="
comparisons you do will still not work the way you expect.

The better way to solve this problem is to use the position() function,
which
returns an integer representing the current count in the for-each loop.  So

the first time through the loop, position() will be 1, the second time will

be 2, and so on.  One way to solv ethe problem is to use this to make sure
that you don't output the semicolon on the first time.

This is also more efficient than comparing the strings, since you are only
comparing a single integer versus comparing every character in the string.

It is also more general should you come across a case where there are
duplicate trade-name elements (filtering out duplicate nodes is an entirely

separate issue).

Another option is to use position() along with the last() function, which
returns an integer that is the last index of the loop.  So if there are 10
trade-name elements selected in the for-each loop, then last() will return
10.  You can compare last() = position() to see if you are on the last time

through the loop.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

  <xsl:template match="foo">
    <annotationsText>

      <variable_line>
        <xsl:for-each select="prescriber-info/trade-name">
          <xsl:if test="position() > 1">; </xsl:if>
          <xsl:value-of select="."/>
        </xsl:for-each>
      </variable_line>

      <!-- another way to do it -->
      <variable_line>
        <xsl:for-each select="prescriber-info/trade-name">
          <xsl:value-of select="."/>
          <xsl:if test="position() != last()">; </xsl:if>
        </xsl:for-each>
      </variable_line>

    </annotationsText>
  </xsl:template>
</xsl:stylesheet>


--
Furthermore, I believe bacon prevents hair loss.
Peter Davis

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list






 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread