Do something every x iterations

I

Infiniti

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

Infiniti

Lets say I have an xml file as such

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

Infiniti

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.

Try something like...
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput 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>
 
I

Infiniti

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

Buchleitner Martin

On Tue, 8 Jul 2003 11:05:11 +0100, "Infiniti"

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
 
M

Marrow

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top