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 April 20th, 2009, 08:58 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Xpath - select following sibling

Hi,

I have a sequence of nodes - para>graphic>para. I want to associate the graphic element with its previous sibling "para". However, if there is not a graphic element, I don't need select it.

Here is the input:
Code:
<root>
    <para>Data1</para>
    <graphic href="img1.gif"/>
    <para>Data2</para>
    <para>Data3</para>
    <para>Data4</para>
    <graphic href="img4.gif"/>
    <para>Data5</para>
    <graphic href="img5.gif"/>
</root>
Stylesheet:
Code:
<xsl:for-each select="root/para">
            <xsl:value-of select="."/>
            <xsl:text> Image for this para - </xsl:text>
            <xsl:value-of select="following-sibling::graphic[1]/@href"/>
            <xsl:text>            
            </xsl:text>
</xsl:for-each>
Current output:
Code:
Data1 Image for this para - img1.gif
            
Data2 Image for this para - img4.gif
            
Data3 Image for this para - img4.gif
            
Data4 Image for this para - img4.gif
            
Data5 Image for this para - img5.gif
Desired output:
Code:
Data1 Image for this para - img1.gif            

Data4 Image for this para - img4.gif

Data5 Image for this para - img5.gif
Thanks for the help.
 
Old April 20th, 2009, 09:03 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you want
<xsl:value-of select="following-sibling::*[1][self::graphic]/@href"/>
respectively you want to check e.g.
Code:
<xsl:if test="following-sibling::*[1][self::graphic]">
Image for this para:
  <xsl:value-of select="following-sibling::*[1][self::graphic]/@href"/>
</xsl:if>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 20th, 2009, 09:11 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Much thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
XPath to get next node (sibling) jeft0nes XSLT 2 August 8th, 2007 08:30 PM
following-sibling. hanzelko XSLT 1 February 2nd, 2007 04:11 PM
XML: How to select a node with ' in it using Xpath guozai BOOK: Professional C#, 2nd and 3rd Editions 1 October 6th, 2006 12:37 PM
MDI Sibling calling Sibling... Nick.Net VB.NET 2002/2003 Basics 1 December 8th, 2003 09:23 PM





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