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 22nd, 2009, 12:53 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default Copy-of between two nodes

Hi All,

Can i know how to copy all nodes between two nodes.

<Req>
<a/>
<b/>
<c/>
<d/>
<e/>
<f/>
<g/>
</Req>

I need to copy all the nodes between <a/> and till the occurence of node <e/>. and also the code shd be flexible not to select if i dont want node <d/> to be copied

Thanks a lot in davance
 
Old October 22nd, 2009, 01:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 2.0 you can use
Code:
<xsl:template match="Req">
  <xsl:copy-of select="node()[. &gt;&gt; current()/a and . &lt;&lt; current()/e]"/>
</xsl:template>
If you want to exclude d then use
Code:
<xsl:template match="Req">
  <xsl:copy-of select="node()[. &gt;&gt; current()/a and . &lt;&lt; current()/e] except d"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; October 22nd, 2009 at 01:05 PM.. Reason: correcting code
 
Old October 22nd, 2009, 01:03 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You could write
Code:
<xsl:copy-of select="f/preceding-sibling::*"/>
, or in XSLT 2.0
Code:
<xsl:variable name="start" select="..."/><xsl:variable name=-"end" select="..."/><xsl:copy-of select="*[. &gt;&gt; $start and . &lt;&lt; $end]"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old October 22nd, 2009, 01:59 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Kay and martin for quick response.I am using XSLTv1.0 Can i know how to do it XSLTv1.0.

As you suggested i can use .

<xsl:copy-of select="f/preceding-sibling::*"/>

but how to put a condition in the same line such tht it should not select node </d>

I need to ouput only nodes between <a> and <f/> where i shd not copy <d/>

thanks
 
Old October 22nd, 2009, 02:12 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 1.0 you can use the intersection of the siblings following a and the siblings preceding e. To exclude d you can add a not(self::d) check:
Code:
<xsl:template match="Req">
  <xsl:variable name="n1" select="a/following-sibling::node()"/>
  <xsl:variable name="n2" select="e/preceding-sibling::node()"/>
  <xsl:copy-of select="$n1[count(. | $n2) = count($n2)][not(self::d)]"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 22nd, 2009, 03:28 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks a lot Martin that worked.

but i didnt understand the code wht its doing here.Can you explain please

<xsl:copy-of select="$n1[count(. | $n2) = count($n2)][not(self::d)]"/>

Thank you
 
Old October 22nd, 2009, 05:03 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's a bit of history here. I claimed in the first edition of my book that there was no way in XSLT 1.0 of forming the intersection of two node-sets $A and $B. A week after it went to print I realized I was wrong and published the solution on the xsl-list at mulberrytech (where a lot of XSLT ideas first appear). If a node $N is in $B, then count($N|$B) is equal to count($B) (because forming the union of two sets using "|" eliminates duplicates). So given a node-set $A, you can apply a filter that selects a node only if it is in $B using the expression
Code:
$A[count(.|$B) = count($B)]
.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to value in b/n nodes Swetha XSLT 1 May 20th, 2008 12:20 PM
How to select these nodes? hustsay23 XSLT 1 September 7th, 2006 04:15 AM
differentiating between two nodes RazoRmedia XSLT 4 February 9th, 2006 06:25 AM
HOW TO: Display specified # of nodes kwilliams XSLT 2 October 5th, 2005 11:04 AM
Need to copy all nodes, attributes, and namespaces juaniux XSLT 4 October 22nd, 2004 04:39 PM





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