Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2008, 04:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default deleting the continuous commas

im trying to get a list of values from set of rows. i separate the result with comma. but if the value is null, an ampty comma is obtained. how to overcome this issue.

see my code below:
<xsl:template match="rattrow[.//UsedByProduct = $implSource][.//TL1Attribute]" mode="positional">
        <xsl:param name="tl1c"/>

        <xsl:variable name="currRow" select="."/>

        <xsl:for-each select=".[.//UsedByProduct = $implSource]">

                    <xsl:choose>
                        <xsl:when test="$tl1c = 'Retrieve'">
                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType='RetrieveIn'], '^[O|Y].*$')]">
                                <xsl:text>[&lt;</xsl:text>
                                <xsl:value-of select=".//TL1Attribute"/>
                                <xsl:text>&gt;]</xsl:text>
                            </xsl:for-each>

                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType='RetrieveIn'], '^M.*$')]">
                                <xsl:text>&lt;</xsl:text>
                                <xsl:value-of select="$currRow//TL1Attribute"/>
                                <xsl:text>&gt;</xsl:text>
                            </xsl:for-each>
                        </xsl:when>

                        <xsl:when test="$tl1c = 'Read'">
                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType='ReadIn'], '^[O|Y].*$')]">
                                <xsl:text>[&lt;</xsl:text>
                                <xsl:value-of select="$currRow//TL1Attribute"/>
                                <xsl:text>&gt;]</xsl:text>
                            </xsl:for-each>

                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType='ReadIn'], '^M.*$')]">
                                <xsl:text>&lt;</xsl:text>
                                <xsl:value-of select="$currRow//TL1Attribute"/>
                                <xsl:text>&gt;</xsl:text>
                            </xsl:for-each>
                        </xsl:when>

                        <xsl:otherwise>
                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType=$tl1c], '^[O|Y].*$')]">
                                <xsl:text>[&lt;</xsl:text>
                                <xsl:value-of select="$currRow//TL1Attribute"/>
                                <xsl:text>&gt;]</xsl:text>
                            </xsl:for-each>

                            <xsl:for-each
                                select=".[matches($currRow//TL1Commands[@cmdType=$tl1c], '^M.*$')]">
                                <xsl:text>&lt;</xsl:text>
                                <xsl:value-of select="$currRow//TL1Attribute"/>
                                <xsl:text>&gt;</xsl:text>
                            </xsl:for-each>
                        </xsl:otherwise>
                    </xsl:choose>
                  </xsl:for-each>

                  <xsl:if test = "not(position()=last())">
                    <xsl:text>, </xsl:text>
                  </xsl:if>
    </xsl:template>

If the above template, checks a list of rows, then it produces the output as as shown below:
<AID>, <AID>, ,
I can omit the last comma, but again if the last()-1 is also empty, then an empty comma is produced.

any help is appreciated



__________________
Rummy
 
Old July 21st, 2008, 09:51 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's some very strange code here.

This:

<xsl:for-each select=".[.//UsedByProduct = $implSource]">

is equivalent to

<xsl:if test=".//UsedByProduct = $implSource">

which is surely clearer. Moreover, that condition seems to be always true, because if it weren't true then the template wouldn't match.

Similarly most people would write

<xsl:for-each select=".[matches($currRow//TL1Commands[@cmdType='RetrieveIn'], '^M.*$')]">

as <xsl:if test="matches(....)">

Moreover the three branches of the xsl:choose are so similar that they could surely be turned into a function with a single parameter.

As for the actual question: position()=last() within a template rule is probably not a particularly good idea, because its meaning depends on the set of things selected by the corresponding apply-templates call, which you haven't shown us. If you want to use position()=last(), then in the apply-templates that invokes this template rule you should only select the elements you want to process: that is, you should filter out those that are (to use your term) "null".

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old July 23rd, 2008, 12:46 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

thank you. i have solved this thro' recursive template.






Similar Threads
Thread Thread Starter Forum Replies Last Post
remove commas and space in a string kumiko Classic ASP Basics 1 January 10th, 2008 11:42 PM
XSLT issue to add commas to values 2BOrNot2B XSLT 3 October 31st, 2006 12:04 PM
ignoring commas in individual fields when parsing clem_c_rock PHP How-To 1 April 20th, 2006 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.