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 July 8th, 2008, 12:01 AM
Registered User
 
Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default xml splitting using XSLT

 Hello.
I have the task to split up an XML message/file into smaller sets of n number of nodes per message/file. The problem that I am having is that I can get the node at the nth spot, but I can't get the subsequent nodes. For example, the xml file could be:
<?xml version...>
<list>
   <student id=1>
      <name>Bill</name>
   </student>
   <student id=2>
      <name>Tom</name>
   </student>
   <student id=3>
      <name>Gerry</name>
   </student>
   <student id=4>
      <name>Anne</name>
   </student>
</list>

I want to group this so that I have:
<?xml version...>
<list>
   <student id=1>
      <name>Bill</name>
   </student>
   <student id=2>
      <name>Tom</name>
   </student>
</list>

and
<xml version=...>
<list>
   <student id=3>
      <name>Gerry</name>
   </student>
   <student id=4>
      <name>Anne</name>
   </student>
</list>

Now, in my xslt, I have:

<xsl:for-each select="//student>
   <xsl:choose>
      <xsl:when test="position() mod 2 = 0">
         ...do something...

If anyone has done this before or knows of a way to do this, please let me know.
Thanks in advance.
K
 
Old July 8th, 2008, 01:45 AM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

first you need to group your elements, you can do this e.g. like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/list">
        <lists>
            <xsl:for-each select="student">
                <xsl:if test="floor(position() div 2) = floor((position()-1) div 2)">
                    <list>
                        <xsl:copy-of select="."/>
                        <xsl:copy-of select="following::student[1]"/>
                    </list>
                </xsl:if>
            </xsl:for-each>
        </lists>
    </xsl:template>
</xsl:stylesheet>
then, as you need separate XMLs probably you can use smth like <xsl:result-document>: http://www.w3.org/TR/xslt20/#element-result-document, which is available from version 2.0

 
Old July 8th, 2008, 02:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default


For groups of size $N

<xsl:template match="list">
  <xsl:for-each select="student[position() mod $N = 0]">
    <list>
      <xsl:copy-of select=".|following-sibling::student[position() &lt; $N]"/>
    </list>
  </xsl:for-each>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 8th, 2008, 02:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Correction:

<xsl:for-each select="student[position() mod $N = 1]"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 8th, 2008, 12:09 PM
Registered User
 
Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much guys. That worked perfectly (I used michael's approach).
Kurt...





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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