473,473 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Grouping problem (or is it?)

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:output 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 '07 #1
8 1530
pa**********@orient.su.se 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/
Mar 21 '07 #2
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
Mar 21 '07 #3
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

Mar 21 '07 #4
p.*****@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

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
Mar 21 '07 #5
On Mar 21, 5:58 pm, Joseph Kesselman
<keshlam-nos...@comcast.netwrote:
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

Mar 22 '07 #6
<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 '07 #7
p.*****@ctncorp.com 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
Mar 22 '07 #8
in message <11**********************@b75g2000hsg.googlegroups .com>,
pa**********@orient.su.se ('pa**********@orient.su.se') 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:output 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>

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

X-no-archive: No, I'm not *that* naive.

Mar 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
1
by: amber | last post by:
Hello, I have a report in VB.NET/Crystal Reports. I have a criteria form that users select between 2 different types of grouping (group by category or group by year). Can I programmatically...
2
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
0
by: Corey | last post by:
hello, I’m trying to run a query and I’m getting error messages Can anyone help me get though this problem? 1: Tried this and got this error message ORA-00923 , decode...
0
by: Roman Bertle | last post by:
Hello, I try to format monetary values using the locale module, python2.5: Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56) on linux2 Type "help", "copyright", "credits" or "license" for...
1
by: Sandeep Singh | last post by:
Hi, How to do group by in XSLT ? I tried on the following codes: <files> <file name="swablr.eps" size="4313" project="mars"/> <file name="batboy.wks" size="424" ...
6
patjones
by: patjones | last post by:
Good afternoon: This seems like it shouldn't be hard, and then again this is how so many problems seem at the outset. My situation is this: I have a report called rptMain319, which is based...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.