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 4th, 2006, 08:27 AM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with position() in nested for-each

Hi! I'm new to the forum, and I'd appreciate if someone can help me with an issue.

I'm transforming an XML into HTML, and want to get something like numbering for chapters.

Here's a simplified example:
____________________________________
<report>
<section name="introduction">
    <section name="prologue">there was a time...</section>
</section>
<section name="conclusion">
    <section name="ending">evil guys win</section>
    <section name="epilogue">bleh</section>
</section>
</report>
-------------------------------------
I want to get something like:

1.-introduction
1.1.-prologue
2.-conclusion
2.1.-ending
2.2.-epilogue

So, I'm using nested for-each:
____________________________________
<xsl:for-each select="report/section">
    <xsl:number value="position()"/>.-
    <xsl:value-of select="@name"/>
    <p/>
    <xsl:for-each select="./section">
     <xsl:number value="position()"/>.-
     <xsl:value-of select="@name"/>
     <p/>
    </xsl:for-each>
</xsl:for-each>
----------------------------

(there might be some other error in what i've just written, but that's not the point)
So, what i get is this:

1.-introduction
1.-prologue
2.-conclusion
1.-ending
2.-epilogue

My problem is about getting the index of the first loop.
How can I get the index (position) of the superior loop? Any ideas?

Thanks very much in advance
 
Old July 4th, 2006, 09:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can store the value of the outer position() in a variable

<xsl:variable name="p" select="position()"/>

and reference it within the inner loop.

However, xsl:number is designed to solve your problem more directly:

<xsl:number level="multiple" count="section"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 5th, 2006, 02:10 AM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much. So simple... I guess the tutorial I read was not very complete ;)






Similar Threads
Thread Thread Starter Forum Replies Last Post
get grandchildrens position() bonekrusher XSLT 2 March 19th, 2008 09:48 AM
Position barski XSLT 5 July 11th, 2007 01:54 PM
window.opener working .... not working alyeng2000 Javascript How-To 5 January 5th, 2007 08:05 AM
Web.Config..Working or Not Working peace95 ASP.NET 1.0 and 1.1 Basics 1 September 18th, 2006 06:53 AM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM





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