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 22nd, 2009, 08:22 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Extracting second chlid element of same parent node using following-sibling

Hi All,

I am trying to pull the second and other following child element 'p' from the parent element 'fn' keeping the first element 'p' as is; but it result in repetition of same child element values.

Input XML

<fn>
<p>Line1</p>
<p>Line2</p>
<p>Line3</p>
</fn>

Used XSLT:

<xsl:template match="//fn">
<xsl:choose>
<xsl:when test="p[following-sibling::p]">
<block>
<xsl:apply-templates/>
</block>
</xsl:when>
</xsl:choose>
<xsl:apply-templates/>
</xsl:template>

Required Ouput:

<fn>
<p>Line1</p>
<block>Line2</block>
<block>Line3</block>
</fn>

Any help would be grateful.
__________________
Thanks,
Rocxy.
 
Old May 22nd, 2009, 08:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't understand your verbal description but the following templates should give you the posted required output:
Code:
  <xsl:template match="fn | fn/p[1]">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="fn/p[position() &gt; 1]">
    <block>
      <xsl:apply-templates/>
    </block>
  </xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old May 22nd, 2009, 08:33 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It all depends on how you want to pick the children elements. There are many ways to do this - the way you have used equates to stating "select all 'p' elements that have a 'p' element following them" - i.e. it will select all apart from the last one - exactly what you don't want.

Far better to simply use the position() xpath function:
Code:
<xsl:template match="fn">
  <fn><xsl:apply-templates/></fn>
</xsl:template>
<xsl:template match="p[1]">
  <p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="p[position() > 1]">
  <block><xsl:apply-templates/></block>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?

Last edited by samjudson; May 22nd, 2009 at 08:37 AM..
 
Old May 22nd, 2009, 10:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think I would do:

Code:
<xsl:template match="p[1]">
  <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="p">
  <block><xsl:copy-of select="child::node()"/></block>
</xsl:template>
__________________
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
Want to Pull multiple following-sibling data to preceding-sibling element sameer_kadam XSLT 4 May 9th, 2009 08:07 AM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
XPath to get next node (sibling) jeft0nes XSLT 2 August 8th, 2007 08:30 PM
Trouble with preceding sibling to a parent steelrose XSLT 5 August 31st, 2006 08:06 AM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 05:10 AM





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