Subject: Re: [xsl] Adding a break character between a group of input data except for the last entry From: Peter Davis <pdavis@xxxxxxxxx> Date: Thu, 29 Nov 2001 16:45:46 -0800 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Adding a break character , Jörg Heinicke | Thread | [xsl] Duplicate emails, Peter Davis |
Re: [xsl] Adding a break character , Jörg Heinicke | Date | [xsl] Duplicate emails, Peter Davis |
Month |