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 February 25th, 2009, 11:25 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default extract nested list

I have the following XML:

<list level="1">
<item>1</item>
<list level="2">
<item>1</item>
<item>2</item>
<item>3</item>
</list>
<item>2</item>
</list>

I would like to extract the nested list and insert it between item 1 and 2 of the parent list:

<list level="1">
<item>1</item>
</list>
<list level="1">
<item>1</item>
<item>2</item>
<item>3</item>
</list>
<list level="1">
<item>2</item>
</list>

Any suggestions?
 
Old February 26th, 2009, 12:45 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:

Code:
 
<xsl:template match="list[@level = 1]">
<list>
<xsl:apply-templates></xsl:apply-templates>
</list>
</xsl:template>

<xsl:template match="list[@level = 1]/item">
<list level="1">
<xsl:copy-of select="."></xsl:copy-of>
</list>
<xsl:apply-templates select="list"></xsl:apply-templates>
</xsl:template>

<xsl:template match="list[@level = 2]">
<xsl:copy-of select="."></xsl:copy-of>
</xsl:template>

__________________
Rummy
 
Old February 26th, 2009, 04:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not shown by your example, but I suspect you want all adjacent items within a list to remain grouped. That makes it a group-adjacent problem:

Code:
<xsl:template match="list">
  <xsl:for-each-group select="*" group-adjacent="node-name(.)">
    <xsl:choose>
      <xsl:when test="self::item">
        <list><xsl:apply-templates select="current-group()"/></list>
      <xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
  </xsl:for-each-group>
</xsl:template>

<xsl:template match="item">
  <xsl:copy-of select="."/>
</xsl:template>
I haven't handled the level numbers there, and I don't know if it does the right thing with an empty list - you didn't specify. But it's a starter.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 26th, 2009, 06:14 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Thank you guys





Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Bullet List tims XSLT 1 May 8th, 2008 06:37 AM
T-SQl Extract gregalb SQL Server 2000 2 March 14th, 2007 12:59 AM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM
List tablesname from database & list databasename ittorget MySQL 3 September 10th, 2005 03:06 AM





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