Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] Recursive positional grouping (was xsl digest...)

From: "Michael Kay" <mike@------------>
To:
Date: 4/4/2007 9:52:00 AM
> 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>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent