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 18th, 2008, 01:54 PM
Authorized User
 
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How not to insert a comma if attribute is false

Hi.

I have the following snippet of XML:
<Names>
  <Name correct="true">John Doe</Name>
  <Name correct="false">Joe Smith</Name>
  <Name correct="true">Jane Doe</Name>
  <Name correct="false">Billy Smith</Name>
</Names>


I like the output after XSL transformation to be the following:
NAMES: John Doe, Jane Doe.


Here is my snippet of XSLT so far:
<xsl:template match=Names">NAMES: <xsl:text/>
  <xsl:call-template name="Name_Template"/>
  <xsl:text>.</xsl:text>
</xsl:template>

<xsl:template name="Name_Template">
  <xsl:for-each select="Name">
     <xsl:if test="@correct='true'">
        <xsl:value-of select="."/>
        <xsl:if test="position()!=last()">
           <xsl:text>, </xsl:text>
        </xsl:if>
     </xsl:if>
  </xsl:for-each>
</xsl:template>


My output so far is the following, which is incorrect:
NAMES: John Doe, Jane Doe,.

The output has a comma beside the period, which I don't want it to be.

How do I modify XSLT to fix this? Any suggestions appreciated.
 
Old July 18th, 2008, 02:13 PM
Authorized User
 
Join Date: Jul 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jane Doe is the third child of <names> ,not the last.

 
Old July 18th, 2008, 02:23 PM
Authorized User
 
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is the last comma I want to get rid of.

I want the output to be:
NAMES: John Doe, Jane Doe.

I don't want the output to be, which is currently doing:
NAMES: John Doe, Jane Doe,.

Remember that the XSLT checks the "correct" attribute before outputting a name.
 
Old July 18th, 2008, 03:10 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I would suggest:

<xsl:template match="Names">
  <xsl:text>NAMES: </xsl:text>
  <xsl:apply-templates select="Name[@correct='true']"/>
  <xsl:text>.</xsl:text>
</xsl:template>

<xsl:template match="Name">
  <xsl:value-of select="."/>
  <xsl:if test="position()!=last()">, </xsl:if>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old July 18th, 2008, 03:13 PM
Authorized User
 
Join Date: Jul 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

maybe it is a method to help you,but i am not sure.
<xsl:template name="Name_Template">
  <xsl:for-each select="Name">
     <xsl:if test="@correct='true'">
        <xsl:value-of select="."/>
        <xsl:choose>
        <xsl:when test="position()!=last() and following-sibling::Name[@correct='true']">

           <xsl:text>, </xsl:text>
         </xsl:when>
        <xsl:otherwise></xsl:otherwise>
         <xsl:choose>
        </xsl:if>
     </xsl:if>
  </xsl:for-each>
</xsl:template>







Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignoring in False clause? gabster XSLT 2 September 7th, 2007 08:29 AM
Boolean True/False to Yes/No peterasimpson VB How-To 2 February 6th, 2006 07:58 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
return (false) crmpicco Javascript 1 October 24th, 2005 11:39 AM
EOF always false with wildcard pacejohn Access VBA 1 August 20th, 2004 12:44 AM





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