[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: generic grouping without nesting for-each-group?

Subject: Re: generic grouping without nesting for-each-group?
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 28 Jul 2010 16:29:53 +0200
Re:  generic grouping without nesting for-each-group?
James Cummings wrote:

I'm trying to process some very flat XML which looks something like:

===
<body>
<head level="1">one</head>
   <p>test</p>
   <p>test</p>
<head level="3">two</head>
   <p>test</p>
   <p>test</p>
<head level="2">three</head>
   <p>test</p>
   <p>test</p>
</body>
===

(But for this case these could easily have been <h1> <h3> and <h2>)

What I want to do, predictably, is give nested structure to this using
@level and get something like:

===
<body>
<div>
  <head level="1>one</head>
  <p>test</p><p>test</p>
  <div>
    <div><head level="3">two</head>
    <p>test</p><p>test</p>
    </div>
   </div>
   <div>
   <head level="2">three</head>
   <p>test</p><p>test</p>
   </div>
</div>
</body>
====
(i.e. notice the extra blank encompassing div around the level 3 division.)

Here is an XSLT 2.0 stylesheet that should do:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf"
  version="2.0">

<xsl:output indent="yes"/>

  <xsl:template match="body">
    <xsl:copy>
      <xsl:sequence select="mf:group(*, 1, 0)"/>
    </xsl:copy>
  </xsl:template>

<xsl:function name="mf:group" as="node()*">
<xsl:param name="elements" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:param name="last-level" as="xs:integer"/>
<xsl:for-each-group select="$elements" group-starting-with="head[@level = $level]">
<xsl:variable name="head" as="element()?" select=".[self::head[@level = $level]]"/>
<xsl:variable name="tail" as="element()*" select="current-group() except $head"/>
<xsl:choose>
<xsl:when test="$head">
<xsl:sequence select="mf:nested-divs($head, $tail, $level, $level - $last-level)"/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$tail[self::head[@level]]">
<xsl:sequence select="mf:group($tail, $level + 1, $last-level)"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$tail"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>


<xsl:function name="mf:nested-divs" as="element()*">
<xsl:param name="head" as="element()?"/>
<xsl:param name="tail" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:param name="i" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$i gt 0">
<div>
<xsl:sequence select="mf:nested-divs($head, $tail, $level, $i - 1)"/>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$head"/>
<xsl:choose>
<xsl:when test="$tail[self::head[@level]]">
<xsl:sequence select="mf:group($tail, $level + 1, $level)"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$tail"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:function>


</xsl:stylesheet>

It has become rather complicated with two functions due to the requirement to add missing levels.

Your latest suggestion to first normalize the input by adding missing levels and then to group in a second pass might indeed be an approach leading to shorter and less complicated code.


--


	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.