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 May 18th, 2008, 04:26 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default first element in array nesting siblings

I've seen it before but can't find the standard solution anymore for creating starting[list]<li> tags after a first element and end tag </li></ul> after the last element of a list.

what I want is looping over an array of anchor tags and convert these anchors in a list of anchors where the first element is nested with all the sibling tags.

input:
    <tr><td><a>first</a><a>2</a><a>3</a><a>4</a><td></tr>
    <tr><td><a>first</a><a>2</a><a>3</a><a>4</a><td></tr>

output[list]
    <li>
        <a> first <a/>
        [list]<li><a>2</a><a>3</a><a>4</a></li></ul>
    <li>
    <li>
        <a> first <a/>
        [list]<li><a>2</a><a>3</a><a>4</a></li></ul>
    </li>
</ul>

I think I can do this with first selecting the first a element and then loop over all its next siblings but don't know if this is the right way to do it. I thought there was also a way where you only loop over all the a tags
 
Old May 18th, 2008, 03:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First you need to change your mindset. You don't create start tags and end tags in XSLT - you create nodes structured as a tree. (In fact you seem to be using the word "tag" interchangeably (a) in its correct sense where[list] and </ul> are two separate tags, a start tag and an end tag, and (b) incorrectly as a synonym for "element".)

In your particular example the solution is actually quite straightforward:

<xsl:template match="td">
  <li><xsl:copy-of select="a[1]"/></li>
  [list]<li><xsl:copy-of select="a[position() > 1]"/></li></ul>
</xsl:template>

Some similar problems are much more difficult, especially in XSLT 1.0: search for "XSLT positional grouping" for details.

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
div element in array won't display onload linus9 BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 5 November 6th, 2008 02:08 AM
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 11:40 AM
Array Element android66 Javascript 2 November 12th, 2004 04:50 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 12:15 PM





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