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 June 6th, 2008, 01:17 PM
Registered User
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML question on div

Hi, here is a simple input of the XML

<FieldGroup>
   <Field>1</Field>
   <Field>2</Field>
   <Field>3</Field>
   <Field>4</Field>
   <Field>5</Field>
   <Field>6</Field>
</FieldGroup>

My intention is to put a <div> for every two Field items I encountered

Output:

<div>
   <Field>1</Field>
   <Field>2</Field>
</div>
<div>
   <Field>3</Field>
   <Field>4</Field>
</div>
<div>
   <Field>5</Field>
   <Field>6</Field>
</div>

Is this something I can accomplish using the for each group in xslt? If not, any suggested approach i can try? Thanks.

 
Old June 6th, 2008, 02:38 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can use

<xsl:for-each-group select="Field" group-adjacent="position()-1 idiv 2">
  <div>
    <xsl:copy-of select="current-group()"/>
  </div>
</xsl:for-each-group>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2008, 03:04 PM
Registered User
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried this; however, the output seems to be the following:

<div>
  <Field>1</Field>
</div>
<div>
  <Field>2</Field>
</div>
<div>
  <Field>3</Field>
</div>
...
..

I appreciated your response, maybe I can manipulate the group-adjacent selection to make it work. Thanks.

 
Old June 6th, 2008, 03:48 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Should be group-adjacent="(position()-1) idiv 2". Sorry for the slip.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2008, 05:15 PM
Registered User
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Mhkay, what I technically want to do is the following.

INput:

<FieldGroup>
   <Field>1</Field>
   <Field>2</Field>
   <Field>3</Field>
</FieldGroup>

I want to surround every two Field items using a single div as before. Yet, insteads of copying the Field items over. I have a template called <xsl:template match="Field"> which i called for every field item. Apparently, with some changes to your method,

<xsl:for-each-group select="Field" group-adjacent="position()-1 idiv 2">
  <div>
    <xsl:apply-templates select="../Field"/>
  </div>
</xsl:for-each-group>

assuming the template do nothing for now

I will get a different result such as

<div>
  <Field>1</Field>
  <Field>2</Field>
  <Field>3</Field>
<div>
<div>
  <Field>1</Field>
  <Field>2</Field>
  <Field>3</Field>
</div>

The result was produced twice. I don't understand what the problem is.

 
Old June 6th, 2008, 05:23 PM
Registered User
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I resolved the issue, thanks very much for your assistance.

 
Old June 6th, 2008, 05:24 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

For each group, you are applying templates not to the Fields in the current-group(), but to all the Fields that are children of the parent of the first field in the group.

Use <xsl:apply-templates select="current-group()"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with positioning a DIV using XSLT and XML skidmerc XSLT 3 May 2nd, 2008 06:45 AM
convert XML to XMl - Another Namespace question bonekrusher XSLT 2 July 10th, 2007 07:32 AM
Rookie question: container div not containing CFGerry BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 6 September 1st, 2005 09:00 PM
Div Question interrupt Javascript How-To 8 July 19th, 2005 08:46 AM
Question about "floating div" JavaScript [email protected] Javascript 0 April 17th, 2005 12:19 PM





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