473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do something every x iterations

Lets say I have an xml file as such

<page>
<chapter>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
</chapter>
</page>

And in my XSL file I want to do something every x occurances of the
paragraph element to end up with something like this. How can I match this ?

Jul 20 '05 #1
6 1964
Lets say I have an xml file as such <page>
<chapter>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
</chapter>
</page> And in my XSL file I want to do something every x occurances of the
paragraph element to end up with something like this. How can I match
this ?

I missed off the example there, but I want to take such an XML file and
apply a stylesheet to get something like this

[page name]
[chapter name]
Three Paragraphs
[paragraph]
[paragraph]
[paragraph]
Three Paragraphs
[paragraph]
[paragraph]
[paragraph]

etc.
Jul 20 '05 #2
Ok I understand that thanks. One thing that makes it more complex is that
the paragraph elements all have different names. I.e., mix of tags, but all
need to be treated as a group.

Hi, Try something like... <?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="grp-size" select="3"/>
<xsl:template match="page">
<xsl:text>[page name] </xsl:text>
<xsl:apply-templates select="chapter"/>
</xsl:template> <xsl:template match="chapter">
<xsl:text>[chapter name] </xsl:text>
<!-- do every n'th paragraph -->
<xsl:apply-templates select="paragraph[position() mod $grp-size = 1
or $grp-size = 1]" mode="grp-start"/>
</xsl:template> <xsl:template match="paragraph" mode="grp-start">
<xsl:value-of select="$grp-size"/>
<xsl:text> paragraphs </xsl:text>
<!-- aplly to this and next n < size -->
<xsl:apply-templates select=". |
following-sibling::paragraph[position()
&lt; $grp-size]"/>
</xsl:template> <xsl:template match="paragraph">
<xsl:text>[paragraph] </xsl:text>
</xsl:template>
</xsl:stylesheet> Hope this helps
Marrow http://www.marrowsoft.com - home of Xselerator (XSLT IDE and
debugger)
http://www.topxml.com/Xselerator "Infiniti" <an*********@nodomain.com> wrote in message
news:o-********************@giganews.com...
Lets say I have an xml file as such <page>
<chapter>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
<paragraph/>
</chapter>
</page> And in my XSL file I want to do something every x occurances of the
paragraph element to end up with something like this. How can I match
this

?




Jul 20 '05 #3
Hi,
Ok I understand that thanks. One thing that makes it more complex is
that the paragraph elements all have different names. I.e., mix of
tags, but

all
need to be treated as a group.

Show an example of the actual XML? ;)


I have components like textbox, etc in the XML file that is generated
automatically from my Java code, and then an XSL stylesheet is applied to
turn it into html files. I can write admin pages for my web site in seconds.
However I wanted to lay out the components in tables, but for that I needed
the above code. I shall just change my code so that all the elements are
like such

<component type="textbox"> instead of <textbox>

Jul 20 '05 #4
Hi,
Ok I understand that thanks. One thing that makes it more complex is
that the paragraph elements all have different names. I.e., mix of
tags, but

all
need to be treated as a group.

Show an example of the actual XML? ;)


It's all working now except when I use 1 as the variable. Then it doesn't
seem to match anything. This is related to the question I just posted.
Jul 20 '05 #5
On Tue, 8 Jul 2003 11:05:11 +0100, "Infiniti"
<an*********@nodomain.com> wrote:

Hi!

does position() begin at 0 or 1 ?
It starts with 1 ..
How can I output

position() mod $variable to the screen ?


I'd say...
<xsl:value-of select="position() mod $variable"/>
....should work - it does with XMLSpy ...

Martin

Jul 20 '05 #6
Hi,
Another question,

does position() begin at 0 or 1 ?
position() is 1 based.
How can I output

position() mod $variable to the screen ?

If I put it inside a value-of block it doesn't do anything.
Probably because position() is subtely different when used inside or outside
a predicate (i.e. [] square brackets).

For example, when you use position() within a predicate like...

<xsl:apply-templates select="somenodes[position() mod 3 = 1]"/>

the position() function returns the position of each node within the
node-set selected by the 'somenodes' part.

Whereas, if you do...

<xsl:value-of select="position()"/>

the position() function is returning the position within the currently
selected (applied or for-each) node-set.

Cheers
Marrow
"Infiniti" <an*********@nodomain.com> wrote in message
news:II********************@giganews.com...
> Hi,

>> Ok I understand that thanks. One thing that makes it more complex is
>> that the paragraph elements all have different names. I.e., mix of
>> tags, but

> all
>> need to be treated as a group.

> Show an example of the actual XML? ;)


Another question,

does position() begin at 0 or 1 ?

How can I output

position() mod $variable to the screen ?

If I put it inside a value-of block it doesn't do anything.

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
7293
by: Derek Basch | last post by:
Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets Thanks, Derek Basch
2
2745
by: Steve Taylor | last post by:
Is there some reason why I shouldn't be able to use the same XPathExpression simultaneously in multiple iterations? The test below illustrates the problem. It seems that creating two...
5
1418
by: hilz | last post by:
Hi all I have this situation where I have an xml file similar to this: <Root> <MyElement year="2004"><Amount>10</Amount></MyElement> <MyElement year="2004"><Amount>11</Amount></MyElement>...
2
2681
by: Steven D'Aprano | last post by:
When using the timeit module, you pass the code you want to time as strings: import timeit t = timeit.Timer("foo(x, y)", \ """from module import foo x = 27 y = 45 """) elapsed_time =...
18
3686
by: Derek Basch | last post by:
What is the best way to count nested loop iterations? I can only figure to use an index but that seems kludgy. index = 0 for animal in zoo: for color in animal: index += 1 Thanks, Derek...
2
1259
by: Francogrex | last post by:
Hi again, with a new question (beginner have a lot to learn). I am doing an updating algorithm where I have to do 10 iterations and each time need to replace the values of the matrix theta by the...
10
2474
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
6
5122
by: John A Grandy | last post by:
Is it possible to write a foreach so that it simultaneously iterates through two collections ( or two properties of the same collection ) ? Following is *only meant as an example* : -- iterate...
0
1340
by: John [H2O] | last post by:
Hello, I have a module created from a Fortran file to read in unformatted binary fortran output. It works fine for some datasets, but crashes with others. The strange thing is it will loop...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.