Re: [xsl] spreadsheet xml and group-starting-with

Subject: Re: [xsl] spreadsheet xml and group-starting-with
From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx>
Date: Thu, 07 Apr 2011 02:39:28 +0200
please see below, near 'group-starting-with'

On 2011-04-07 01:35, Fred Christian wrote:
I have a MS spreadsheet xml that I will be transforming into an html table.
I need to use group-starting-with to group multiple rows into one
based on empty cells in the first column.

ex. If I have

Col A, Col b, Col c
1a, 1b, 1c
    ,  2b, 2c
    ,      ,3c
4a,4b,4c
     ,    ,5c
---
I will eventually create a html table with two rows and three columns.
For now I am trying to just get grouping to work, but I got stuck.
Just when I thought I had for-each-group figured out I get stumped again.
Can you get me going in the right direction?

My xslt
---------------------------------------------------------------------
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:output encoding="iso-8859-1" method="xml" indent="yes"/>

<xsl:template match="ss:Workbook">
<body>
      <xsl:apply-templates select="ss:Worksheet[1]/ss:Table"/>
</body>
</xsl:template>

<xsl:template match="ss:Table">
	<xsl:for-each select="ss:Row">
		<xsl:if test="ss:Cell[1][not(@ss:Index)]"><data>grouping row</data></xsl:if>
	</xsl:for-each>
<data>show them grouped now</data>
	<xsl:for-each-group select="ss:Row"
group-starting-with="ss:Cell[1][not(@ss:Index)]">

This is a pattern. You have to specify at which element the grouping should start, just as you'd specify a template's match:
group-starting-with="ss:Row[not(ss:Cell[1]/@ss:Index)]">


		<group>
		<xsl:copy-of select="current-group()"/>
		</group>
	</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
---------------------------------------------------------------------
End My xslt

For this example, I want output that looks something like the following.
It doesn't have to be exact, but if I can get two groups with the
implied cell data, then I can probably go from there, I hope.
---------------------------------------------

A complete stylesheet with @Index-Cell expansion may look like:


<?xml version="1.0" encoding="utf-8"?>
  <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    xmlns:saxon="http://saxon.sf.net/";
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:html4="http://www.w3.org/TR/REC-html40";
    xmlns:html="http://www.w3.org/1999/xhtml";
    exclude-result-prefixes="saxon xs o x ss html4 html"
    >

<xsl:output
method="xml"
indent="yes"
saxon:suppress-indentation="term definition image reference extrainfo"
/>


    <xsl:template match="/">
      <xsl:apply-templates select="ss:Workbook/ss:Worksheet[1]/ss:Table"/>
    </xsl:template>

    <xsl:template match="ss:Workbook">
      <body>
        <xsl:apply-templates select="ss:Worksheet[1]/ss:Table"/>
      </body>
    </xsl:template>

<xsl:template match="ss:Table">
<xsl:for-each select="ss:Row">
<xsl:if test="ss:Cell[1][not(@ss:Index)]"><data>grouping row</data></xsl:if>
</xsl:for-each>
<data>show them grouped now</data>
<xsl:for-each-group select="ss:Row"
group-starting-with="ss:Row[not(ss:Cell[1]/@ss:Index)]">
<group>
<xsl:apply-templates select="current-group()" mode="expand-indexed-cells" />
</group>
</xsl:for-each-group>
</xsl:template>



<xsl:template match="ss:Cell[@ss:Index]" mode="expand-indexed-cells" >
<xsl:variable name="last-index-item" select="preceding-sibling::ss:Cell[@ss:Index][1]" as="element(ss:Cell)?"/>
<xsl:variable name="last-pos" select="if ($last-index-item)
then count(preceding-sibling::ss:Cell[. &gt;&gt; $last-index-item]) + $last-index-item/@ss:Index
else count(preceding-sibling::ss:Cell)" />
<xsl:for-each select="( (($last-pos + 1) cast as xs:integer) to ((@ss:Index - 1) cast as xs:integer))">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" />
</xsl:for-each>
<xsl:copy>
<xsl:copy-of select="@* except @ss:Index" />
<xsl:apply-templates mode="#current" />
</xsl:copy>
</xsl:template>



<xsl:template match="@* | *" mode="#all" priority="-20">
<xsl:copy>
<xsl:apply-templates select="@* | node() | comment()" mode="#current"/>
</xsl:copy>
</xsl:template>


</xsl:stylesheet>


-Gerrit



-- Gerrit Imsieke Geschdftsf|hrer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschdftsf|hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vvckler

Current Thread