Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Do something every x iterations

Reply
Thread Tools

Do something every x iterations

 
 
Infiniti
Guest
Posts: n/a
 
      07-07-2003
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 ?



 
Reply With Quote
 
 
 
 
Infiniti
Guest
Posts: n/a
 
      07-07-2003

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


 
Reply With Quote
 
 
 
 
Infiniti
Guest
Posts: n/a
 
      07-07-2003
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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput method="text"/>
> <xslaram 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:aragraph[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" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> 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

> ?








 
Reply With Quote
 
Infiniti
Guest
Posts: n/a
 
      07-08-2003

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



 
Reply With Quote
 
Infiniti
Guest
Posts: n/a
 
      07-08-2003

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


 
Reply With Quote
 
Buchleitner Martin
Guest
Posts: n/a
 
      07-08-2003
On Tue, 8 Jul 2003 11:05:11 +0100, "Infiniti"
<(E-Mail Removed)> 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

 
Reply With Quote
 
Marrow
Guest
Posts: n/a
 
      07-08-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> > 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trees, iterations and adding leaves vertigo Python 1 01-02-2007 04:25 PM
Counting nested loop iterations Derek Basch Python 18 03-17-2006 09:21 PM
xslt challenging situation related to iterations hilz XML 5 02-16-2006 01:20 AM
Counting iterations Derek Basch Python 11 04-11-2005 08:05 PM
Struts iterations Gordon Macleod Java 0 07-21-2003 08:28 PM



Advertisments