Grouping problem (or is it?)

P

patrik.nyman

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
 
M

Martin Honnen

<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.
 
J

Joseph Kesselman

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.
 
P

p.lepin

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.
 
J

Joseph Kesselman

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 said:
> <xsl:apply-templates
> select="item[following-sibling::cb]"/>

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

p.lepin

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.
 
P

patrik.nyman

<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
 
J

Joseph Kesselman

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.
 
S

Simon Brooke

in message <[email protected]>,
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top