|
Subject: RE: [xsl] Recursive positional grouping (was xsl digest...) From: "Michael Kay" <mike@xxxxxxxxxxxx> Date: Wed, 4 Apr 2007 10:51:28 +0100 |
> Thanks. (The hint in XSLT 2.0, 3Ed, p298 is wrong then).
Good catch.
>
> Using
> <xsl:template match="*[starts-with(name(),'h')]">
> <xsl:variable name="tag"
> select="translate(name(current()),'1234','2345')"/>
> <xsl:message>
> Match on <xsl:value-of select="name()"/>
> Population is <xsl:value-of select="count(current-group())"/>
> next is <xsl:value-of select="$tag"/>
> </xsl:message>
> <section level="{substring-after(name(),'h')}">
> <head><xsl:apply-templates/></head>
> <xsl:for-each-group select="current-group() except ."
> group-starting-with="*[name() = $tag]">
> <xsl:apply-templates select="current-group()"/>
> </xsl:for-each-group>
> </section>
> </xsl:template>
>
> Still doesn't hack it though.
> It seems to handle the lower levels,
> but not something like
> h3
> h3
> h2
>
I think your problem is that
<xsl:apply-templates select="current-group()"/>
is processing all the elements in a group, not only the next-level headings
- so the paragraphs are output at every level.
I think this kind of recursive grouping works best if you use a named
template that is passed the contents of a group as a parameter. Try
something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template name="group">
<xsl:param name="pop" as="element()*"/>
<xsl:param name="level" as="xs:integer" />
<xsl:variable name="key" as="xs:string" select="concat('h', $level)"/>
<xsl:for-each-group select="$pop" group-starting-with="*[name() eq
$key]">
<xsl:choose>
<xsl:when test="starts-with(name(.), 'h')">
<section level="{$level}">
<head><xsl:apply-templates select="current-group()[1]"/></head>
<xsl:call-template name="group">
<xsl:with-param name="pop" select="remove(current-group(),
1)"/>
<xsl:with-param name="level" select="$level + 1"/>
</xsl:call-template>
</section>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="/">
<doc>
<xsl:call-template name="group">
<xsl:with-param name="pop" select="body/*"/>
<xsl:with-param name="level" select="1"/>
</xsl:call-template>
</doc>
</xsl:template>
</xsl:stylesheet>
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] Re: xsl-list Digest 4 Apr, Abel Braaksma | Thread | [xsl] Re: Dreamweaver XSL Tranforms, Mark Anderson |
| Re: [xsl] Xsl:variable content shou, Abel Braaksma | Date | RE: [xsl] Xsl:variable content shou, Michael Kay |
| Month |