Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 30th, 2007, 07:47 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incrementing attribute value

Hi,

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:


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

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

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

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


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.



Pankaj
__________________
Pankaj
Reply With Quote
  #2 (permalink)  
Old October 30th, 2007, 09:44 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
Reply With Quote
  #3 (permalink)  
Old October 30th, 2007, 02:11 PM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

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>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

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

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old October 31st, 2007, 05:29 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default


<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.

Pankaj
Reply With Quote
  #5 (permalink)  
Old October 31st, 2007, 05:40 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apologies, I mean

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

Pankaj
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:23 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.