Do something every x iterations

Discussion in 'XML' started by Infiniti, Jul 7, 2003.

  1. Infiniti

    Infiniti Guest

    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 ?
    Infiniti, Jul 7, 2003
    #1
    1. Advertising

  2. Infiniti

    Infiniti Guest


    > 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.
    Infiniti, Jul 7, 2003
    #2
    1. Advertising

  3. Infiniti

    Infiniti Guest

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


    > Hope this helps
    > Marrow http://www.marrowsoft.com - home of Xselerator (XSLT IDE and
    > debugger)
    > http://www.topxml.com/Xselerator


    > "Infiniti" <> wrote in message
    > news:eek:...
    >> 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

    > ?
    Infiniti, Jul 7, 2003
    #3
  4. Infiniti

    Infiniti Guest


    > 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>
    Infiniti, Jul 8, 2003
    #4
  5. Infiniti

    Infiniti Guest


    > 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.
    Infiniti, Jul 8, 2003
    #5
  6. On Tue, 8 Jul 2003 11:05:11 +0100, "Infiniti"
    <> 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
    Buchleitner Martin, Jul 8, 2003
    #6
  7. Infiniti

    Marrow Guest

    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" <> wrote in message
    news:...
    >
    > > 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.
    >
    >
    Marrow, Jul 8, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Macleod

    Struts iterations

    Gordon Macleod, Jul 21, 2003, in forum: Java
    Replies:
    0
    Views:
    406
    Gordon Macleod
    Jul 21, 2003
  2. hilz
    Replies:
    5
    Views:
    534
    Joe Kesselman
    Feb 16, 2006
  3. Derek Basch

    Counting iterations

    Derek Basch, Apr 8, 2005, in forum: Python
    Replies:
    11
    Views:
    577
    Andrew Dalke
    Apr 11, 2005
  4. Derek Basch

    Counting nested loop iterations

    Derek Basch, Mar 16, 2006, in forum: Python
    Replies:
    18
    Views:
    836
    Scott David Daniels
    Mar 17, 2006
  5. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,452
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
Loading...

Share This Page