[xsl] sort & modulo

Discussion in 'XML' started by Tjerk Wolterink, Apr 21, 2005.

  1. I posted my problem earlier, but i simplified the examples,
    and i know what the cause of the problem is, but i dont know the solution,

    my xml file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="datastyle.xsl"?>
    <data xmlns="http://tjerk.com">
    <item>a1</item>
    <item>b2</item>
    <item>c3</item>
    <item>d4</item>
    <item>e5</item>
    <item>f6</item>
    <item>a0</item>
    </data>


    If i would sort the text items elements i would place the
    last item a0 at the beginning. But i am also
    using the modulo operator to create a table like structure:

    The xsl file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:tjerk="http://tjerk.com"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>
    <xsl:param name="cols" select="3"/>

    <xsl:template match="/tjerk:data">
    <data>
    <xsl:for-each select="./tjerk:item[(position() mod $cols)=1]">
    <xsl:sort data-type="text" select="tjerk:item" order="descending" />
    <tr>
    <xsl:apply-templates select="."/>
    <xsl:apply-templates select="following-sibling::tjerk:item[position() &lt;
    $cols]"/>
    </tr>
    </xsl:for-each>
    </data>
    </xsl:template>

    <xsl:template match="tjerk:item">
    <td><xsl:value-of select="."/></td>
    </xsl:template>
    </xsl:stylesheet>

    ---



    The output is:
    -
    <data>
    <tr><td>a0</td></tr>
    <tr><td>a1</td><td>b0</td><td>c3</td></tr>
    <tr><td>d4</td><td>e5</td><td>f6</td></tr>
    </data>
    -

    But i want the output to be like this:
    -
    <data>
    <tr><td>a0</td><td>a1</td><td>b0</td></tr>
    <tr><td>c3</td><td>d4</td><td>e5</td></tr>
    <tr><td>f6</td></tr>
    </data>
    -

    The problem is the sort element in the foreach loop,

    how can i solve this???
    Help is much appreciated.
    Tjerk Wolterink, Apr 21, 2005
    #1
    1. Advertising

  2. Use two pass transformation -- the first to sort and the second to group.

    Cheers,
    Dimitre Novatchev

    "Tjerk Wolterink" <> wrote in message
    news:d48cp1$76q$...
    >I posted my problem earlier, but i simplified the examples,
    > and i know what the cause of the problem is, but i dont know the solution,
    >
    > my xml file:
    >
    > <?xml version="1.0" encoding="ISO-8859-1"?>
    > <?xml-stylesheet type="text/xsl" href="datastyle.xsl"?>
    > <data xmlns="http://tjerk.com">
    > <item>a1</item>
    > <item>b2</item>
    > <item>c3</item>
    > <item>d4</item>
    > <item>e5</item>
    > <item>f6</item>
    > <item>a0</item>
    > </data>
    >
    >
    > If i would sort the text items elements i would place the
    > last item a0 at the beginning. But i am also
    > using the modulo operator to create a table like structure:
    >
    > The xsl file:
    >
    > <?xml version="1.0" encoding="ISO-8859-1"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:tjerk="http://tjerk.com"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:eek:utput method="xml" indent="yes"/>
    > <xsl:param name="cols" select="3"/>
    >
    > <xsl:template match="/tjerk:data">
    > <data>
    > <xsl:for-each select="./tjerk:item[(position() mod $cols)=1]">
    > <xsl:sort data-type="text" select="tjerk:item" order="descending" />
    > <tr>
    > <xsl:apply-templates select="."/>
    > <xsl:apply-templates select="following-sibling::tjerk:item[position() &lt;
    > $cols]"/>
    > </tr>
    > </xsl:for-each>
    > </data>
    > </xsl:template>
    >
    > <xsl:template match="tjerk:item">
    > <td><xsl:value-of select="."/></td>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > ---
    >
    >
    >
    > The output is:
    > -
    > <data>
    > <tr><td>a0</td></tr>
    > <tr><td>a1</td><td>b0</td><td>c3</td></tr>
    > <tr><td>d4</td><td>e5</td><td>f6</td></tr>
    > </data>
    > -
    >
    > But i want the output to be like this:
    > -
    > <data>
    > <tr><td>a0</td><td>a1</td><td>b0</td></tr>
    > <tr><td>c3</td><td>d4</td><td>e5</td></tr>
    > <tr><td>f6</td></tr>
    > </data>
    > -
    >
    > The problem is the sort element in the foreach loop,
    >
    > how can i solve this???
    > Help is much appreciated.
    Dimitre Novatchev, Apr 21, 2005
    #2
    1. Advertising

  3. Dimitre Novatchev wrote:
    > Use two pass transformation -- the first to sort and the second to group.
    >
    > Cheers,
    > Dimitre Novatchev



    Ok but can i do that with just one invocation of the xsltprocessor??
    I do not have acces to the xslt processor, and it just calls the xml with xsl
    transformation one time, so is there no other solution??
    Or can i do a twopass transformmation in one xsltransformation??



    >
    > "Tjerk Wolterink" <> wrote in message
    > news:d48cp1$76q$...
    >
    >>I posted my problem earlier, but i simplified the examples,
    >>and i know what the cause of the problem is, but i dont know the solution,
    >>
    >>my xml file:
    >>
    >><?xml version="1.0" encoding="ISO-8859-1"?>
    >><?xml-stylesheet type="text/xsl" href="datastyle.xsl"?>
    >><data xmlns="http://tjerk.com">
    >><item>a1</item>
    >><item>b2</item>
    >><item>c3</item>
    >><item>d4</item>
    >><item>e5</item>
    >><item>f6</item>
    >><item>a0</item>
    >></data>
    >>
    >>
    >>If i would sort the text items elements i would place the
    >>last item a0 at the beginning. But i am also
    >>using the modulo operator to create a table like structure:
    >>
    >>The xsl file:
    >>
    >><?xml version="1.0" encoding="ISO-8859-1"?>
    >><xsl:stylesheet version="1.0"
    >>xmlns:tjerk="http://tjerk.com"
    >>xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >>
    >><xsl:eek:utput method="xml" indent="yes"/>
    >><xsl:param name="cols" select="3"/>
    >>
    >><xsl:template match="/tjerk:data">
    >><data>
    >><xsl:for-each select="./tjerk:item[(position() mod $cols)=1]">
    >><xsl:sort data-type="text" select="tjerk:item" order="descending" />
    >><tr>
    >><xsl:apply-templates select="."/>
    >><xsl:apply-templates select="following-sibling::tjerk:item[position() &lt;
    >>$cols]"/>
    >></tr>
    >></xsl:for-each>
    >></data>
    >></xsl:template>
    >>
    >><xsl:template match="tjerk:item">
    >><td><xsl:value-of select="."/></td>
    >></xsl:template>
    >></xsl:stylesheet>
    >>
    >>---
    >>
    >>
    >>
    >>The output is:
    >>-
    >><data>
    >><tr><td>a0</td></tr>
    >><tr><td>a1</td><td>b0</td><td>c3</td></tr>
    >><tr><td>d4</td><td>e5</td><td>f6</td></tr>
    >></data>
    >>-
    >>
    >>But i want the output to be like this:
    >>-
    >><data>
    >><tr><td>a0</td><td>a1</td><td>b0</td></tr>
    >><tr><td>c3</td><td>d4</td><td>e5</td></tr>
    >><tr><td>f6</td></tr>
    >></data>
    >>-
    >>
    >>The problem is the sort element in the foreach loop,
    >>
    >>how can i solve this???
    >>Help is much appreciated.

    >
    >
    >
    Tjerk Wolterink, Apr 22, 2005
    #3
  4. "Tjerk Wolterink" <> wrote in message
    news:d4ambq$u9k$...
    > Dimitre Novatchev wrote:
    >> Use two pass transformation -- the first to sort and the second to group.
    >>
    >> Cheers,
    >> Dimitre Novatchev

    >
    >
    > Ok but can i do that with just one invocation of the xsltprocessor??


    Yes.

    > I do not have acces to the xslt processor, and it just calls the xml with
    > xsl transformation one time, so is there no other solution??
    > Or can i do a twopass transformmation in one xsltransformation??


    Yes.

    The result of the first transformation is produced within the content (body)
    of an xsl:variable.

    Then this RTF is converted to an intermediary tree using the xxx:node-set()
    extension function (available with almost every XSLT processor).

    Then the second transformation is applied on this intermediary tree.

    See for example:

    "A Generic template for multi-pass processing (Was: Re: Applying two
    transformations consecutively)"

    http://www.biglist.com/lists/xsl-list/archives/200106/msg01152.html


    Cheers,
    Dimitre Novatchev.
    Dimitre Novatchev, Apr 22, 2005
    #4
    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. Christian Kruggel

    General modulo-question

    Christian Kruggel, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    446
    Brad BARCLAY
    Jul 7, 2003
  2. nobody
    Replies:
    0
    Views:
    536
    nobody
    Jun 1, 2004
  3. Replies:
    1
    Views:
    3,600
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    2
    Views:
    9,265
    Martin Honnen
    Sep 5, 2006
  5. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page