[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Grouping Problems XSLT2.0


Subject: Re: [xsl] Grouping Problems XSLT2.0
From: Jeff Sese <jsese@xxxxxxxxxxxx>
Date: Wed, 14 Mar 2007 18:34:33 +0800

Thanks David, it does the job. I was able to do the grouping for the subsections, but the the epigraph was the one that really bugged me. Then after seeing your post, i read the spec and saw that using group-adjacent requires an expression which makes the replace a nice work around for my problem (And for sure I would not think of this even after reading the spec, I always tend to use patterns and not think of expressions).

Again thanks a lot.
--
Jeff

David Carlisle wrote:
I don't know, there's something about the grouping attributes in
xsl-for-each-group I just don't seem to get, perhaps Michael could
comment?
Most of XSLT2 I just absorb and hardly ever refer to the spec, but I
_always_ have to check up the definition of for-each-group to check
which grouping attributes take a pattern, and which take an expression.
In the example below, the first, using group-adjacent, takes an
expression, but the second, using group-starting-with takes a pattern.
As is usally the case I got that wrong first time round. I must be
missing the "idea" behind this split, usually with XSL if you think
about things the right way, the xslt solution seesm perfectly natural,
it's just a matter of getting the brain to think the right way....


David




<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>

  <xsl:template match="section">
    <section>
      <xsl:for-each-group select="para" group-adjacent="replace(@class,'source','para')">
	<xsl:choose>
	  <xsl:when test="current-grouping-key() = 'epigraphpara'">
	    <epigraph>
	      <xsl:apply-templates select="current-group()"/>
	    </epigraph>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:for-each-group select="current-group()" group-starting-with="para[inline[ends-with(@class,'title')]]">
	    <subsection>
	      <title><xsl:apply-templates select="."/></title>
	      <xsl:apply-templates select="current-group()[position()!=1]"/>
	    </subsection>
	    </xsl:for-each-group>
	  </xsl:otherwise>	
	</xsl:choose>
      </xsl:for-each-group>
    </section>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>


</xsl:stylesheet>





$ saxon8 epi.xml epi.xsl <?xml version="1.0" encoding="UTF-8"?> <section> <epigraph> <para class="epigraphpara">...</para> <para class="epigraphpara">...</para> <para class="epigraphpara">...</para> <para class="epigraphpara">...</para> <para class="epigraphsource">...</para> </epigraph> <subsection> <title> <para class="subsection1para"> <inline class="subsection1title">...</inline> </para> </title> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> </subsection> <subsection> <title> <para class="subsection1para"> <inline class="subsection1title">...</inline> </para> </title> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> <para class="subsection1para">...</para> </subsection> </section>


Current Thread
Keywords