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 28th, 2005, 12:36 PM
Authorized User
 
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional adding

Hey, i am sorry that i kept asking all of these adding questions. I am trying to number all of the elements of the same type. However, if an element has a certian attribute, then add the attribute and kept counting as normal. Here would be a given input:

<a/>
<a/>
<a/>
<a val="2"/>
<a/>
<a/>
<a val="3"/>
<a/>

and i would like to get this output somehow.
 <b number="1"/>
 <b number="2"/>
 <b number="3"/>
 <b number="5"/>
 <b number="6"/>
 <b number="7"/>
 <b number="10"/>
 <b number="1!"/>

Thanks in advance to anyone that can help!

 
Old July 28th, 2005, 01:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you're not worried about efficiency, then

<xsl:template match="a">
  <b number="{count((.|preceding-sibling::a)[not(@val)]) +
                sum((.|preceding-sibling::a)/@val)}"/>
</xsl:template>

For a more efficient solution you need a recursive template that walks over the siblings accumulating a running total as it goes.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional transformation vvenk XSLT 3 July 10th, 2008 09:09 AM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM
Conditional submit arnabghosh Javascript How-To 1 August 4th, 2005 02:40 AM
Conditional IF statement iloveoatmeal Classic ASP Basics 5 July 20th, 2005 08:33 PM





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