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 August 2nd, 2013, 07:35 AM
Registered User
 
Join Date: Aug 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to display 2 records per row in XSLT (code included)

I want to display 2 records per row from a list in my SharePoint site.

I am using XSLT 1.0. This is how I want to display records:

<tr>
<td>Record1</td>
<td>Record2</td>
</tr>
<tr>
<td>Record3</td>
<td>Record4</td>
</tr>

I have written the following code to do this job but getting error on line where <tr> is written. Basically XSLT is not allowing me to write <tr> without closing </tr>. But I want it that way otherwise my logic won't work. So is there a way to achieve this?

CODE

<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<tr> (this is my error line)
</xsl:when>
</xsl:choose>

<td>rest of my code will come here</td>

<xsl:choose>
<xsl:when test="position() mod 2 = 0">
</tr>
</xsl:when>
</xsl:choose>
 
Old August 2nd, 2013, 08:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're thinking in terms of writing tags to the serialized output. But that's not what XSLT does: it builds a tree. Writing a node is an atomic operation, you can't write two halves of a node to a tree separately.

The logic you want is something like this:

<xsl:if test="position() mod 2 = 1">
<tr>
process "."
process "./following-sibing::*[1]"
</tr>
</xsl:if>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 2nd, 2013, 09:26 AM
Registered User
 
Join Date: Aug 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I can change my logic slightly so the code looks like this.

<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<tr>
<REST OF THE CODE HERE>
</tr>
</xsl:when>
</xsl:choose>

But since now I will display two records in one operation, I need to know how to loop through two records at a time.

For e.g. in PHP we can use something like

(for $i=0; $i<count($records); $i+2)

Notice $i+2 in loop above as it will now loop through 2 records at a time.
 
Old August 2nd, 2013, 09:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

if they are siblings, use the technique I showed you: process "." and "./following-sibling::*[1]".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 2nd, 2013, 11:30 AM
Registered User
 
Join Date: Aug 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What is "1" in code below?

"./following-sibling::*[1]"

Can I use my column name here? I tried using "Title" and "@Title" instead of "1" but nothing was displayed on screen. "Title" is the name of column which I want to display.
 
Old August 2nd, 2013, 12:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>What is "1" in code below?

You're abusing this forum. Get yourself a book on XSLT and XPath, read it, and come back here if there are things you don't understand.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 2nd, 2013, 01:01 PM
Registered User
 
Join Date: Aug 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know if asking question is abusing forum. Anyway I have already solved my problem so thanks but no thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT - xmlns namespace included in the output Arch82 XSLT 2 July 16th, 2013 02:45 PM
java included in xslt savantKing XSLT 11 January 25th, 2012 10:34 AM
apply-template from included xslt stylesheet chobo XSLT 9 April 16th, 2008 05:25 PM
Included XSLT and use as object YoungLuke C# 2 October 31st, 2007 04:40 AM
Display multiple records / row in DataGrid angshujit ASP.NET 2.0 Basics 0 January 18th, 2007 07:03 AM





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