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 October 30th, 2007, 07:47 AM
Authorized User
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incrementing attribute value


I've a very specific requirement in my style sheet, wherein I need to increment (lexical) the attribute values of particular element, for instance “bba”, “bbb”, “bbc” and so on. Let me try to explain with a sample. Below is how my markup looks like:

                     <p type=”box”>xxxx </p> (The attribute value should be “bba”)

               <p type=”box”>yyy</p> (The attribute value should be “bbb”)
                      <p type=”box”>zzz</p> (The attribute value should be “bbc”)

                         <p>kkk</p> (The attribute value should be “bbd”)

                    <p>ppp</p> (The attribute value should be “bbe”)

I’ve tried to use count(//p[@type=’box’]) etc but that I can feel is not correct way of doing to get the desired results. I also cannot use the preceding-sibling/following-sibling sort of things, as the <p> can occur anywhere in <fm>, <body> or <bm> parts or may be only in one part as many times.

I was thinking of using of counter sort of things to manipulate the data, but have no idea whether it is feasible in XSLT. Can anybody guide me in this regard or may let which part of XSLT tutorial I should look into to get this work for me.

Just to add, I’ve fair knowledge of XPath and using 1.0 version on MAC.

Thanks for guidance in advance.

Old October 30th, 2007, 09:44 AM
samjudson's Avatar
Friend of Wrox
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

You can't 'increment' values in XSLT, as XSLT is a side-effect-free programming language. There is usually another way of doing what you are trying to do however.

Depending on the format of your attributes you might get some luck out of the <xsl:number> instruction.

/- Sam Judson : Wrox Technical Editor -/
Old October 30th, 2007, 02:11 PM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

The solution to this depends on what exactly you mean by "for instance bba, bbb, bbc, bbd etc". Depending on exacly how prescriptive you are about this sequence, you may or may not be able to use xsl:number.

It's not clear to me whether you are showing your input or your desired output. Perhaps you want to copy the input to the output with the addition of these serial "numbers"? You can do this with an identity transform supplemented by a numbering rule: something like:

<xsl:template match="*">
    <xsl:copy-of select="@*"/>

<xsl:template match="p">
  <p><xsl:number level="any" format="a"/></p>

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old October 31st, 2007, 05:29 AM
Authorized User
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts

<xsl:attribute name="att">bb<xsl:number level="any" format="a"/></xsl:attribute>
is what I was looking for. I have given a thought of <xsl:number> but had doubt, of using it in <xsl:number>.

I would have given a try first before asking. Anyways, thanks a ton.

Old October 31st, 2007, 05:40 AM
Authorized User
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts

Apologies, I mean

 I have given a thought of <xsl:number> but had doubt, of using it in <xsl:attribute>.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing numbers RubyRue ASP.NET 1.0 and 1.1 Basics 2 January 30th, 2008 12:16 AM
Incrementing a value within a template Tre XSLT 1 March 21st, 2007 09:31 AM
For Loop Not incrementing donrafeal7 Javascript 1 October 24th, 2006 12:24 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 12:04 PM

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