# Re: Output fixed length records using mod operators to

 Subject: Re: Output fixed length records using mod operators to group results From: David Carlisle Date: Fri, 11 May 2007 10:26:01 +0100
```>      <xsl:if test="position()=last()">

tests if you are the last item. An alternative woiuld be to just put the
code after the for-each except then you'd have to count the items again
whereas inside the loop you have the total number of items as last()

>        <xsl:for-each select="(//*)[position()&lt;= ((4000 -last() )mod
>        4)]">

<xsl:for-each select="(//*)[position()&lt;= n]
is a well known xslt1 trick way of doing what in xslt2 would be
<xsl:for-each select="1 to n"
(//*) just selects a bunch of nodes it doesn't matter which ones (so
long as the input dociment has enough nodes) then (//*)[position()&lt;=
n] iterates n times, so in this case adds n  blank \$fields.

So what's n?

you want a number between 0 and 3 inclusive which when added to last()
makes a multiple of 4, so if there were 13 items you want 3 so that the
total is 16.

"mod" is "clock arithmetic" but (here) using 4 rather than 12.
"x is a multiple of 4" is "x = 0 mod 4"
so the value you want for n is -x as -x + x = 0 mod 4.
but if there are 13 items -13 mod 4 is -1, and while it's true that
-1 = 3 mod 4, you definitely want to add three blank fields rather than
delete your last item in order to get the right multiple. So you just
need to make sure that the value will be positive.  Adding a large
multiple of 4 is just a trick way of doing that (not safe of course if
there may be more than 4000 entries. A safer and perhaps clearer way but
taking a few more keystrokes would be
(4 - (last() mod 4)) mod 4
so going back to the "13" example, that would be
(4 - (13 mod 4) mod 4 = (4 - 1) mod 4 = 3
instead of what i wrote which is
((4000 -13 )mod 4 = 3987 mod 4 = 3

>          <xsl:value-of select="\$field"/>

a blank field

>        </xsl:for-each>
>      </xsl:if>

stuff.

oh by the way as a test that you understand this one, you need to add
the same thing to the po loop (using 2 instead of 4) to add a blank
field in the case there is an odd number of po. Although in that case
you don't need a loop it's just a simple if test that if last() mod 2 =1
add a blank field.

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

```
Current Thread

### PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

### Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
 RSS 2.0 Atom 0.3
 Site Map | Privacy Policy | Terms of Use | Trademarks Free Stylus Studio XML Training:
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.