By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,661 Members | 1,917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,661 IT Pros & Developers. It's quick & easy.

Grouping problem (or is it?)

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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: n/a
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

P: n/a
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

P: n/a
<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: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.