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

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

October 22nd, 2009, 01:00 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
With XSLT 2.0 you can use
Code:
<xsl:template match="Req">
<xsl:copy-of select="node()[. >> current()/a and . << current()/e]"/>
</xsl:template>
If you want to exclude d then use
Code:
<xsl:template match="Req">
<xsl:copy-of select="node()[. >> current()/a and . << 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
|
|

October 22nd, 2009, 01:03 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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="*[. >> $start and . << $end]"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

October 22nd, 2009, 01:59 PM
|
|
Authorized User
|
|
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 22nd, 2009, 02:12 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

October 22nd, 2009, 03:28 PM
|
|
Authorized User
|
|
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 22nd, 2009, 05:03 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
 |