DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: xsl if within for-each

  1. #1
    Curt Guest

    xsl if within for-each



    I wish to display a comma separated list of values, when there are values
    matching a criteria, and it seems that I have tried every combination I can
    think of and none of them work, so I ask for some help and guidance.

    here is a sample of the XML I am looking at

    <NODE>
    <CK></CK>
    <CK>Item 1</CK>
    <CK></CK>
    <CK>Item 2</CK>
    <CK></CK>
    <CK></CK>
    <CK>Item 3</CK>
    <CK></CK>
    <OTHER></OTHER>
    </NODE>

    I want the display to be
    Item1, Item2, Item3

    so far I have the following xsl code segment, but I cannot get the if part
    to work on the current node...

    <xsl:for-each select="CK">
    <xsl:if ????? current value != '' ???? >
    <xsl:value-of /><xsl:if test="context()[not(end())]">, </xsl:if>
    </xsl:if>
    </xsl:for-each>

    Thanks for any help

    Curt

  2. #2
    Russell Jones Guest

    Re: xsl if within for-each

    Try this. Note that I'm using the XSLT namespace not the XSL namespace and
    I'm using the msxml3 parser installed in replace mode.

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xslutput method="text" omit-xml-declaration="yes"/>
    <xsl:template match="/">
    <xsl:for-each select="NODE/CK">
    <xsl:if test="text() != ''">
    <xsl:choose>
    <xsl:when test="position() = last">
    <xsl:value-of select="."/>
    </xsl:when>
    <xsltherwise>
    <xsl:value-of select="."/>,
    </xsltherwise>
    </xsl:choose>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    Russell Jones
    Sr. Web Development Editor
    DevX.com


    "Curt" <Curtm@humboldtBank.com> wrote in message
    news:3a80462c$1@news.devx.com...
    >
    >
    > I wish to display a comma separated list of values, when there are values
    > matching a criteria, and it seems that I have tried every combination I

    can
    > think of and none of them work, so I ask for some help and guidance.
    >
    > here is a sample of the XML I am looking at
    >
    > <NODE>
    > <CK></CK>
    > <CK>Item 1</CK>
    > <CK></CK>
    > <CK>Item 2</CK>
    > <CK></CK>
    > <CK></CK>
    > <CK>Item 3</CK>
    > <CK></CK>
    > <OTHER></OTHER>
    > </NODE>
    >
    > I want the display to be
    > Item1, Item2, Item3
    >
    > so far I have the following xsl code segment, but I cannot get the if

    part
    > to work on the current node...
    >
    > <xsl:for-each select="CK">
    > <xsl:if ????? current value != '' ???? >
    > <xsl:value-of /><xsl:if test="context()[not(end())]">, </xsl:if>
    > </xsl:if>
    > </xsl:for-each>
    >
    > Thanks for any help
    >
    > Curt




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center