 |
| 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
|
|
|
|

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

June 6th, 2008, 02:38 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

June 6th, 2008, 03:04 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 6th, 2008, 03:48 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

June 6th, 2008, 05:15 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 6th, 2008, 05:23 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I resolved the issue, thanks very much for your assistance.
|
|

June 6th, 2008, 05:24 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
 |