Grouping problem (or is it?)

Discussion in 'XML' started by patrik.nyman@orient.su.se, Mar 21, 2007.

  1. Guest

    Consider the following document:

    <?xml version="1.0"?>
    <!DOCTYPE test>
    <test>
    <list type="index">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <cb/>
    <item>D</item>
    <item>E</item>
    <item>F</item>
    </list>
    </test>

    I want to transform this to the following html:

    <table class="index">
    <td class="leftcolumn">
    <p class="item">A</p>
    <p class="item">B</p>
    <p class="item">C</p>
    </td>
    <td class="rightcolumn">
    <p class="item">D</p>
    <p class="item">E</p>
    <p class="item">F</p>
    </td>
    </table>

    For this I've been trying the following style sheet:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" indent="yes"/>

    <xsl:template match="list[@type='index']">
    <table class="index">
    <td class="leftcolumn">
    <xsl:for-each-group select="item"
    group-ending-with="item[following-sibling::cb]">
    <xsl:apply-templates select="current-group()"/>
    </xsl:for-each-group>
    </td>
    <td class="rightcolumn">
    <xsl:for-each-group select="item"
    group-starting-with="item[preceding-sibling::cb]">
    <xsl:apply-templates select="current-group()"/>
    </xsl:for-each-group>
    </td>
    </table>
    </xsl:template>

    <xsl:template match="item">
    <p class="item"><xsl:apply-templates/></p>
    </xsl:template>

    <xsl:template match="cb">
    <xsl:copy/>
    </xsl:template>

    </xsl:stylesheet>

    But this produces the following result:

    <table class="index">
    <td class="leftcolumn">
    <p class="item">A</p>
    <p class="item">B</p>
    <p class="item">C</p>
    <p class="item">D</p>
    <p class="item">E</p>
    <p class="item">F</p>
    </td>
    <td class="rightcolumn">
    <p class="item">A</p>
    <p class="item">B</p>
    <p class="item">C</p>
    <p class="item">D</p>
    <p class="item">E</p>
    <p class="item">F</p>
    </td>
    </table>

    Could someone please tell me what I'm doing wrong?
    Thanks.

    /Patrik Nyman
    , Mar 21, 2007
    #1
    1. Advertising

  2. wrote:

    > <xsl:template match="list[@type='index']">
    > <table class="index">
    > <td class="leftcolumn">
    > <xsl:for-each-group select="item"
    > group-ending-with="item[following-sibling::cb]">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>
    > </td>
    > <td class="rightcolumn">
    > <xsl:for-each-group select="item"
    > group-starting-with="item[preceding-sibling::cb]">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>
    > </td>
    > </table>
    > </xsl:template>


    Simply use

    <xsl:template match="list[@type='index']">
    <table class="index">
    <td class="leftcolumn">
    <xsl:apply-templates select="item[following-sibling::cb]"/>
    </td>
    <td class="rightcolumn">
    <xsl:apply-templates select="item[preceding-sibling::cb]"/>
    </td>
    </table>
    </xsl:template>

    You don't need xsl:for-each-group and the way you tried does not help as
    you get two groups but process them all the same.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 21, 2007
    #2
    1. Advertising

  3. Why not just use:

    <xsl:for-each select="item[following-sibling::cb]">
    <xsl:apply-templates select="."/>
    </xsl:for-each>

    and likewise for preceeding sibling? Everything before the cb will be
    processed in one pass, everything after it in the other. No need for
    grouping, no dependency on XSLT 2.0.

    If you have multiple cb's this becomes more complicated, but your sketch
    didn't handle that either.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Mar 21, 2007
    #3
  4. Guest

    On Mar 21, 5:12 pm, wrote:
    > For this I've been trying the following style sheet:


    I think you're seriously confused. Consider reading some
    sort of XSLT2 reference/tutorial (and let's hope Joseph
    mentions the link to IBM's collection of articles on XSLT
    again--it's not in my bookmarks for some reson).

    > <xsl:for-each-group select="item"
    > group-ending-with="item[following-sibling::cb]">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>


    Have you tried:

    <xsl:apply-templates
    select="item[following-sibling::cb]"/>

    instead?

    > Could someone please tell me what I'm doing wrong?


    I think you're trying to use a feature for feature's sake
    where there's absolutely no need to do that.

    --
    Pavel Lepin
    , Mar 21, 2007
    #4
  5. wrote:
    > I think you're seriously confused. Consider reading some
    > sort of XSLT2 reference/tutorial (and let's hope Joseph
    > mentions the link to IBM's collection of articles on XSLT
    > again--it's not in my bookmarks for some reson).


    The shortcut should be easy to remember: http://www.ibm.com/xml

    That'll redirect you to the XML section of the DeveloperWorks website,
    which is where I usually recommend folks start when looking for
    tutorials and articles. (Yes, I'm biased, but it really is a good
    collection, and surprisingly independent... sometimes more independent
    than I'd prefer, actually. <smile/>)

    > <xsl:apply-templates
    > select="item[following-sibling::cb]"/>


    Blush. That's a cleaner answer than mine; I got distracted by the for-each.



    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Mar 21, 2007
    #5
  6. Guest

    On Mar 21, 5:58 pm, Joseph Kesselman
    <> wrote:
    > wrote:
    > > I think you're seriously confused. Consider reading
    > > some sort of XSLT2 reference/tutorial (and let's hope
    > > Joseph mentions the link to IBM's collection of
    > > articles on XSLT again--it's not in my bookmarks for
    > > some reson).

    >
    > The shortcut should be easy to
    > remember:http://www.ibm.com/xml


    Ah... that must be the reason it's not in my bookmarks. I
    think I'll stuff it there anyway; I've managed to forget it
    after all.

    > That'll redirect you to the XML section of the
    > DeveloperWorks website, which is where I usually
    > recommend folks start when looking for tutorials and
    > articles. (Yes, I'm biased, but it really is a good
    > collection, and surprisingly independent... sometimes
    > more independent than I'd prefer, actually. <smile/>)


    It also might be a bit overwhelming for people new to XML,
    I suppose. I mean, there's an awful lot of useful stuff
    there, it's just that finding precisely the useful stuff
    you need at the moment might be a bit of a problem, so that
    it's more useful for long-term studying purposes than for
    'HALP! I'm up to my neck in it over here' situations.

    --
    Pavel Lepin
    , Mar 22, 2007
    #6
  7. Guest

    > <xsl:apply-templates
    > select="item[following-sibling::cb]"/>


    Ah, how simple! I confess to being confused.
    Thanks all for your input, it's really
    appreciated.

    /Patrik
    , Mar 22, 2007
    #7
  8. wrote:
    > It also might be a bit overwhelming for people new to XML,
    > I suppose. I mean, there's an awful lot of useful stuff
    > there, it's just that finding precisely the useful stuff
    > you need at the moment might be a bit of a problem, so that
    > it's more useful for long-term studying purposes than for
    > 'HALP! I'm up to my neck in it over here' situations.


    The "New to XML" item on the menu bar looks like a good starting point
    if you don't yet know what you don't know <smile/> -- it's a brief
    overview of concepts with a set of links in each sectionfor folks who
    want to dive in deeper on that issue.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Mar 22, 2007
    #8
  9. Simon Brooke Guest

    in message <>,
    ('') wrote:

    > Consider the following document:
    >
    > <?xml version="1.0"?>
    > <!DOCTYPE test>
    > <test>
    > <list type="index">
    > <item>A</item>
    > <item>B</item>
    > <item>C</item>
    > <cb/>
    > <item>D</item>
    > <item>E</item>
    > <item>F</item>
    > </list>
    > </test>
    >
    > I want to transform this to the following html:
    >
    > <table class="index">
    > <td class="leftcolumn">
    > <p class="item">A</p>
    > <p class="item">B</p>
    > <p class="item">C</p>
    > </td>
    > <td class="rightcolumn">
    > <p class="item">D</p>
    > <p class="item">E</p>
    > <p class="item">F</p>
    > </td>
    > </table>
    >
    > For this I've been trying the following style sheet:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <xsl:stylesheet version="2.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="html" indent="yes"/>
    >
    > <xsl:template match="list[@type='index']">
    > <table class="index">
    > <td class="leftcolumn">
    > <xsl:for-each-group select="item"
    > group-ending-with="item[following-sibling::cb]">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>
    > </td>
    > <td class="rightcolumn">
    > <xsl:for-each-group select="item"
    > group-starting-with="item[preceding-sibling::cb]">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>
    > </td>
    > </table>
    > </xsl:template>
    >
    > <xsl:template match="item">
    > <p class="item"><xsl:apply-templates/></p>
    > </xsl:template>
    >
    > <xsl:template match="cb">
    > <xsl:copy/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > But this produces the following result:
    >
    > <table class="index">
    > <td class="leftcolumn">
    > <p class="item">A</p>
    > <p class="item">B</p>
    > <p class="item">C</p>
    > <p class="item">D</p>
    > <p class="item">E</p>
    > <p class="item">F</p>
    > </td>
    > <td class="rightcolumn">
    > <p class="item">A</p>
    > <p class="item">B</p>
    > <p class="item">C</p>
    > <p class="item">D</p>
    > <p class="item">E</p>
    > <p class="item">F</p>
    > </td>
    > </table>
    >
    > Could someone please tell me what I'm doing wrong?
    > Thanks.


    No, because I wouldn't do it like that. Either you want to generate

    <test>
    <list type="index">
    <cb>
    <item>A</item>
    <item>B</item>
    <item>C</item>
    </cb>
    <cb>
    <item>D</item>
    <item>E</item>
    <item>F</item>
    <cb>
    </list>
    </test>

    or you want

    <xsl:variable name="split" select="count( item)/2"/>
    <div class="leftcolumn">
    <xsl:apply-templates select="item[position() &lt;= $split]"/>
    </div>
    <div class="rightcolumn">
    <xsl:apply-templates select="item[position() &gt; $split]"/>
    </div>

    Alternately you could do something like:

    <div class="contentcolumn">
    <xsl:apply-templates select="//story[ not( @lead) and (position()
    mod 3) = 0]">
    <xsl:sort select="created[position()=1]/@iso-8601"
    order="descending"/>
    </xsl:apply-templates>
    </div>
    <div class="contentcolumn">
    <xsl:apply-templates select="//story[ not( @lead) and (position()
    mod 3) = 1]">
    <xsl:sort select="created[position()=1]/@iso-8601"
    order="descending"/>
    </xsl:apply-templates>
    </div>
    <div class="contentcolumn">
    <xsl:apply-templates select="//story[ not( @lead) and (position()
    mod 3) = 2]">
    <xsl:sort select="created[position()=1]/@iso-8601"
    order="descending"/>
    </xsl:apply-templates>
    </div>

    Yup, that's a genuine example. It does this:

    <URL:http://www.stewartry-wheelers.org/wheelers/news>

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    X-no-archive: No, I'm not *that* naive.
    Simon Brooke, Mar 23, 2007
    #9
    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. Bryce (Work)

    Grouping and Sum problem

    Bryce (Work), Sep 30, 2003, in forum: XML
    Replies:
    3
    Views:
    503
    Bryce (Work)
    Oct 1, 2003
  2. Mike King

    Grouping problem

    Mike King, Jan 6, 2005, in forum: XML
    Replies:
    5
    Views:
    448
    David Carlisle
    Jan 6, 2005
  3. virendra
    Replies:
    0
    Views:
    1,116
    virendra
    Aug 9, 2006
  4. I.M. Postor
    Replies:
    2
    Views:
    596
    I.M. Postor
    Sep 27, 2006
  5. Replies:
    8
    Views:
    591
    Pavel Lepin
    Sep 13, 2007
Loading...

Share This Page