|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: A better way to build a comma-delimited list
Hi Joerg,
> <xsl:for-each select="$dbdef[key('dbdef-cols', $dbcolumn)]">
> <xsl:value-of select="$dbcolumn"/>
> <xsl:if test="position() != last()">
> <xsl:text>, </xsl:text>
> </xsl:if>
> </xsl:for-each>
I loved the solution because it would fix the problem AND make the
code shorter. Unfortunately it doesn't work because position() and
last() are both 1 so I get:
select col1col2col3
John
> -----Original Message-----
> From: Joerg Heinicke <joerg.heinicke@xxxxxx>
> Sent: Wednesday, May 15, 2002, 3:15:02 AM,
> Subject: A better way to build a comma-delimited list
> Hello John,
> > <xsl:for-each select="$dbdef">
> > <xsl:if test="key('dbdef-cols', $dbcolumn)">
> > <xsl:value-of select="$dbcolumn"/>
> > <xsl:if test="$p < $l">
> > <xsl:text>, </xsl:text>
> > </xsl:if>
> > </xsl:if>
> > </xsl:for-each>
> instead of the for-each + if you can write one for-each with predicate:
> <xsl:for-each select="$dbdef[key('dbdef-cols', $dbcolumn)]">
> Now the correct nodeset is selected and position() and last() will work.
> But they must refer to the inner for-each, so you can't use $p and $l,
> but <xsl:if test="position() != last()">.
> So, you have then
> <xsl:for-each select="$dbdef[key('dbdef-cols', $dbcolumn)]">
> <xsl:value-of select="$dbcolumn"/>
> <xsl:if test="position() != last()">
> <xsl:text>, </xsl:text>
> </xsl:if>
> </xsl:for-each>
> Regards,
> Joerg
> John Sands schrieb:
>> I have some XSLT that builds a SQL select statement from several input
>> documents. Here's a fragment that builds the column list by getting
>> each distinct column name from a separate file ($pbsdef) and checking
>> that is is defined in another file (referenced by variable $dbdef).
>>
>> <xsl:text>select </xsl:text>
>> <xsl:for-each select="document($pbsdef)/pbsdef/column[not(@dbcolumn=preceding-sibling::column/@dbcolumn)]">
>> <xsl:variable name="p" select="position()"/>
>> <xsl:variable name="l" select="last()"/>
>> <xsl:variable name="dbcolumn" select="@dbcolumn"/>
>> <xsl:for-each select="$dbdef">
>> <xsl:if test="key('dbdef-cols', $dbcolumn)">
>> <xsl:value-of select="$dbcolumn"/>
>> <xsl:if test="$p < $l">
>> <xsl:text>, </xsl:text>
>> </xsl:if>
>> </xsl:if>
>> </xsl:for-each>
>> </xsl:for-each>
>>
>> The output of this fragment might be:
>>
>> select col1, col2, col3
>>
>> This all works fine, except for one case - where the last column is
>> not found in the $dbdef file (where the 'if test=key' fails). Then I
>> get:
>>
>> select col1, col2,
>>
>> The position() < last() test doesn't work here. Can someone see a
>> better way to avoid that last comma?
>>
>> Thanks,
>> John Sands
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








