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

Re: [xsl] Grouping problem. 2.0


Subject: Re: [xsl] Grouping problem. 2.0
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 4 Jul 2008 17:29:05 +0100

> Start all over again then.
> On a Friday. :-(

As it is time to leave on Friday I haven't checked this but......

<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/"
    exclude-result-prefixes="xs saxon">

<xsl:output indent="yes"/>

<xsl:template match="/">
	<bk>
		<xsl:variable name="blocks">
		  <xsl:for-each-group select="/book/*" group-starting-with="*[self::title]">
				  <block>
					  	<xsl:copy-of select="current-group()"/>
				  </block>	
		  </xsl:for-each-group>
	  </xsl:variable>
		
		<xsl:variable name="nested">
			<xsl:call-template name="nest">
				<xsl:with-param name="nodes" select="$blocks//block"/>	
			</xsl:call-template>
		</xsl:variable>	
	
	  <xsl:apply-templates select="$nested/*"/>
	</bk>	
</xsl:template>

<xsl:template name="nest">
	<xsl:param name="nodes" select="/.."/>
	<xsl:param name="level" select="1" as="xs:integer"/>
	<xsl:for-each-group select="$nodes"
group-starting-with="*[title/@role = $level]">
		<group depth="{$level}">
			<xsl:copy-of select="current-group()[1]"/>
			<xsl:call-template name="nest">
				<xsl:with-param name="nodes" select="current-group()[position() > 1]"/>
				<xsl:with-param name="level" select="$level + 1"/>
			</xsl:call-template>	
		</group>
	</xsl:for-each-group>
</xsl:template>
	
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="group">
	<xsl:apply-templates/>	
</xsl:template>

<xsl:template match="title">
	<xsl:copy>
		<xsl:copy-of select="@*"/>
		<xsl:attribute name="sect">
			<xsl:number count="group" level="multiple"/>	
		</xsl:attribute>
		<xsl:apply-templates/>	
	</xsl:copy>	
</xsl:template>

</xsl:stylesheet>




generates this output:


<?xml version="1.0" encoding="UTF-8"?>
<bk>
   <block>
      <title role="1" sect="1">L1</title>
   </block>
   <block>
      <title role="2" sect="1.1">L1.1</title>
      <para>This document defines an XML schema for  ....</para>
   </block>
   <block>
      <title role="2" sect="1.2">L1.2</title>
      <para>The normative XML Schema for the OpenDoc ....</para>
   </block>
   <block>
      <title role="1" sect="2">L2</title>
      <para>This chapter introduces the structure of ....</para>
      <para>In the OpenDocument format, each structu ....</para>
   </block>
   <block>
      <title role="2" sect="2.1">L2.1</title>
      <para>A document root element is the primary e ....</para>
   </block>
   <block>
      <title role="3" sect="2.1.1">L2.1.1</title>
      <para>The content models of the five root elem ....</para>
   </block>
   <block>
      <title role="4" sect="2.1.1.1">L4 test</title>
      <para>The content models of the five root elem ....</para>
      <para>The &lt;office:document&gt; root contains a co ....</para>
   </block>
   <block>
      <title role="4" sect="2.1.1.2">L4 test 2.1.1.2</title>
      <para>The content models of the five root elem ....</para>
      <para>The &lt;office:document&gt; root contains a co ....</para>
   </block>
   <block>
      <title role="2" sect="2.2">L2.1</title>
      <para>A document root element is the primary e ....</para>
   </block>
</bk>


from this input:




<book >
  <title role="1">L1</title>

  <title role="2">L1.1</title>
  <para>This document defines an XML schema for  ....</para>
  <title role="2">L1.2</title>
  <para>The normative XML Schema for the OpenDoc ....</para>
  <title role="1">L2</title>
  <para>This chapter introduces the structure of ....</para>
  <para>In the OpenDocument format, each structu ....</para>

  <title role="2">L2.1</title>
  <para>A document root element is the primary e ....</para>


  <title role="3">L2.1.1</title>
  <para>The content models of the five root elem ....</para>

  <title role="4">L4 test</title>
  <para>The content models of the five root elem ....</para>
  <para>The &lt;office:document&gt; root contains a co ....</para>

 <title role="4">L4 test 2.1.1.2</title>
  <para>The content models of the five root elem ....</para>
  <para>The &lt;office:document&gt; root contains a co ....</para>

 <title role="2">L2.1</title>
  <para>A document root element is the primary e ....</para>

</book>


cheeeeers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/


Current Thread
Keywords