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 28th, 2011, 11:07 AM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy xslt help neede to get value from repeated sibling

Hi,

my xml looks like this

<sop_content>
<method_content>
<main_content/>

<main_image/>
<main_image_width/>
<main_image_height/>
<main_image_alt/>

<main_image/>
<main_image_width/>
<main_image_height/>
<main_image_alt/>

<main_image/>
<main_image_width/>
<main_image_height/>
<main_image_alt/>

</method_content>
</sop_content>

while i am trying to transfrom using for-each main_content & get value from following sibling main_image, I can see the first following sibling main_image only what if i have to show all the main_Images's next to the main_content.

this is how current xslt looks like

<td class="content" colspan="2">
<table class="Contents" WIDTH="100%" border="1" cellspacing="0">
<xsl:for-each select="//method_content/main_content">
<tr>
<td class="contents" style="width: 250px" align="center" valign="middle">

<FONT face="Arial" size="6" font-weight= "bold">
<xsl:number level="any" count="method_content"/>
</FONT>
</td>
<xsl:variable name="Image" select="following-sibling::main_image[position() = 1]"/>
<xsl:variable name="mainImageWidth" select="following-sibling::main_image_width[position() = 1]"/>
<xsl:variable name="mainImageHeight" select="following-sibling::main_image_height[position() = 1]"/>
<xsl:variable name="mainImageAlt" select="following-sibling::main_image_alt[position() = 1]"/>
<FONT face="Arial" size="2">
<td style="width: 450px" border="1"><xsl:copy-of select="."/></td>
</FONT>
<td style="width: 450px" border="1">
<xsl:if test="contains($Image,'/')">
<img src="/dctm-act{$Image}" width="{$mainImageWidth}" height="{$mainImageHeight}" alt="{$mainImageAlt}"/>
</xsl:if>

</td>
</tr>
</xsl:for-each>
</table>
</td>

I am not good at xslt..any help is appreciated ...Thanks...
 
Old April 28th, 2011, 11:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

First, following-sibling::main_image[1] is shorthand - there is no need for the 'position() = ' part of the predicate.

If you are using XSLT 2.0 then youwould probably use xsl:for-each-group with the group-starting-with attribute to split everything using the main_content element.

In XSLT 1.0 You could do something like this:

Code:
<xsl:for-each select="following-sibling::main_image[current()/generate-id()=preceding-sibling::main_content/generate-id()]">
This would give you all the main_image elements where the preceding main_content had the same unique id as the current element (i.e. they are the same).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 28th, 2011, 12:04 PM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default is this gonna work

<td class="content" colspan="2">
<table class="Contents" WIDTH="100%" border="1" cellspacing="0">
<xsl:for-each select="//method_content/main_content">
<tr>
<td class="contents" style="width: 250px" align="center" valign="middle">

<FONT face="Arial" size="6" font-weight= "bold">
<xsl:number level="any" count="method_content"/>
</FONT>
</td>

<xsl:for-each select="following-sibling::main_image[current()/generate-id()=preceding-sibling::main_content/generate-id()]">

<FONT face="Arial" size="2">
<td style="width: 450px" border="1"><xsl:copy-of select="."/></td>
</FONT>
<td style="width: 450px" border="1">
<xsl:if test="contains($Image,'/')">
<img src="/dctm-act{$Image}" />
</xsl:if>

</td>
</xsl:for-each></tr>
</xsl:for-each>
</table>
</td>


but when i add this thing in the xslt , it didnt worked.. Thanks
 
Old April 28th, 2011, 12:15 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well $Image would need to be declared, or replaced by '.' (to signify the current element).

e.g.:

Code:
<xsl:if test="contains(., '/')"> ...
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 29th, 2011, 10:39 AM
bug bug is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy what if have to move all the repeating main_image into one <td>

Hi,


I am bit confused, as i said for one main content there will be many main_image's what if i have to add all the main_images follwing the main_content to one td. Sorry if i am confusing you. Thanks

<tr>
<td>
<table>
<xsl:for-each select="//method_content/main_content">
<tr>
<xsl:variable name="Image" select="following-sibling::main_image[position() = 1]"/>
<xsl:variable name="mainImageWidth" select="following-sibling::main_image_width[position() = 1]"/>
<xsl:variable name="mainImageHeight" select="following-sibling::main_image_height[position() = 1]"/>
<xsl:variable name="mainImageAlt" select="following-sibling::main_image_alt[position() = 1]"/>
</td>

<xsl:copy-of select="main_content"/>

</td>

<td>
<xsl:if test="contains($Image,'/')">
<img src="/dctm-act{$Image}" width="{$mainImageWidth}" height="{$mainImageHeight}" alt="{$mainImageAlt}"/>
</xsl:if>
</td>

</tr>
</xsl:for-each>
</table>
</td>
</tr>
 
Old April 29th, 2011, 05:09 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

...then you need to put the for-each inside the <td>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





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
XSLT Not Pulling Sibling Data? BobSSC XSLT 4 April 23rd, 2009 01:46 PM
matches without repeated items pongup BOOK: Beginning Regular Expressions 0 April 7th, 2007 09:05 PM
An error that has been repeated many times over Giuda BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 October 31st, 2006 06:05 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.