Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Grouping problem (or is it?)

Reply
Thread Tools

Grouping problem (or is it?)

 
 
patrik.nyman@orient.su.se
Guest
Posts: n/a
 
      03-21-2007
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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-21-2007
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/
 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-21-2007
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
 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      03-21-2007
On Mar 21, 5:12 pm, patrik.ny...@orient.su.se 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

 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-21-2007
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
 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      03-22-2007
On Mar 21, 5:58 pm, Joseph Kesselman
<keshlam-nos...@comcast.net> wrote:
> p.le...@ctncorp.com 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

 
Reply With Quote
 
patrik.nyman@orient.su.se
Guest
Posts: n/a
 
      03-22-2007
> <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

 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-22-2007
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
 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      03-23-2007
in message < .com>,
('') 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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.

 
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
XSLT embedding (positional grouping key?) problem. newbie I.M. Postor XML 2 09-27-2006 08:03 AM
problem with crystal report 9 for dynamic multilevel grouping in postback of page virendra ASP .Net 0 08-09-2006 11:40 AM
Grouping problem Mike King XML 5 01-06-2005 05:08 PM
Port Grouping and Trunking problem hari Cisco 7 01-07-2004 09:01 AM
Grouping and Sum problem Bryce (Work) XML 3 10-01-2003 04:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57