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

Re: Grouping a sequence of elements in different ways

Subject: Re: Grouping a sequence of elements in different ways in the same stylesheet
From: Peter Desjardins <peter.desjardins.us@xxxxxxxxx>
Date: Mon, 24 May 2010 11:53:43 -0400
Re:  Grouping a sequence of elements in different ways
Thanks to all for the help. I'll work with these suggestions.

Peter

On Fri, May 21, 2010 at 5:50 PM, Imsieke, Gerrit, le-tex
<gerrit.imsieke@xxxxxxxxx> wrote:
>
>
> On 21.05.2010 23:09, Peter Desjardins wrote:
>>
>> Can anyone point me to an example of that sort of stylesheet? One that
>> performs several types of grouping during the same pass over a
>> sequence of elements? Here's an example of the code I'm using. Both of
>> the grouping operations will work if I use them alone.
>>
>> <xsl:template match = "body/div">
>>     <xsl:for-each-group select = "current-group()" group-adjacent =
>> "name()='p' and @class='bulletedlist'">
>>         <xsl:apply-templates select = "." mode = "itemizedlist" />
>>     </xsl:for-each-group>
>>     <!-- How do I perform both of these grouping operations at the
>> same time? -->
>>     <xsl:for-each-group select = "*" group-starting-with =
>> "p[@class='head1']">
>>         <xsl:apply-templates select="." mode="group-level-one"/>
>>     </xsl:for-each-group>
>> </xsl:template>
>
> It's ok to group in a single pass, but not at the same time. You should use
> nested groupings. Section hierarchy (group-starting-with) comes first, then
> the sections' contents will be grouped according to whether they are list
> items or not.
>
> Here's a sample XHTML body as input:
>
> ===========8<-----------------------
> <?xml version="1.0" encoding="utf-8"?>
> <body>
> <p>pre</p>
> <p class="bulletedlist">one</p>
> <p class="bulletedlist">two</p>
> <p class="bulletedlist">three</p>
> <h5>This is h5</h5>
> <h4>Attribution</h4>
> <p>...</p>
> <h3>Preface</h3>
> <p class="orderedlist">1. one</p>
> <p class="orderedlist">2. two</p>
> <p class="bulletedlist">one</p>
> <p class="bulletedlist">two</p>
> <p class="bulletedlist">three</p>
> <p class="orderedlist">3. three</p>
> <h4>[Preface]</h4>
> <p>...</p>
> <h3>INTRODUCTION</h3>
> <p>...</p>
> <p class="bulletedlist">one</p>
> <p class="bulletedlist">two</p>
> <h5>baz</h5>
> <p>foo</p>
> </body>
> ===========8<-----------------------
>
> Here's a stylesheet to process the input
>
> ===========8<-----------------------
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>  xmlns:my="my"
>  version="2.0"
>  exclude-result-prefixes="my xs"
>  >
>
>  <xsl:output method="xml" indent="yes" />
>
>  <xsl:template match="/">
>    <xsl:apply-templates/>
>  </xsl:template>
>
>  <xsl:template match="body">
>    <xsl:copy>
>      <xsl:sequence select="my:hierarchize(*)" />
>    </xsl:copy>
>  </xsl:template>
>
>  <xsl:function name="my:hierarchize" as="element(*)*">
>    <xsl:param name="nodes" as="element(*)*" />
>    <xsl:for-each-group select="$nodes"
>        group-starting-with="*[my:isHeading(.)][
>                                my:hlevel(.) le min(
>                                  (
>                                    for $ph in (
>                                      preceding-sibling::*
>                                      intersect $nodes[my:isHeading(.)]
>                                    )
>                                    return my:hlevel($ph),
>                                    6
>                                  )
>                                )
>                              ]">
>      <xsl:choose>
>        <xsl:when test="my:isHeading(.)">
>          <div class="level{my:hlevel(.)}">
>            <title>
> <!-- please note that node() are all nodes of the context
>     item, which is the group's first element, i.e., the
>     heading: -->
>              <xsl:copy-of select="node()" />
>            </title>
> <!-- will be called recursively: -->
>            <xsl:sequence
>              select="my:hierarchize(current-group()[position() gt 1])" />
>          </div>
>        </xsl:when>
>        <xsl:otherwise>
> <!-- until no heading is in the nodes to process.
>     Then start processing the lists: -->
>          <xsl:sequence select="my:listify(current-group())" />
>        </xsl:otherwise>
>      </xsl:choose>
>    </xsl:for-each-group>
>  </xsl:function>
>
>  <xsl:function name="my:listify" as="element(*)*">
>    <xsl:param name="nodes" as="element(*)*" />
>    <xsl:for-each-group select="$nodes"
>        group-adjacent="boolean(self::p[@class='bulletedlist'])">
>      <xsl:choose>
>        <xsl:when test="current-grouping-key()">
>          <itemizedlist>
>            <xsl:sequence select="current-group()" />
>          </itemizedlist>
>        </xsl:when>
>        <xsl:otherwise>
>          <xsl:sequence select="current-group()" />
>        </xsl:otherwise>
>      </xsl:choose>
>    </xsl:for-each-group>
>  </xsl:function>
>
>  <xsl:function name="my:isHeading" as="xs:boolean">
>    <xsl:param name="elt" as="element(*)" />
>    <xsl:value-of select="matches(local-name($elt), '^h\d$')" />
>  </xsl:function>
>
>  <xsl:function name="my:hlevel" as="xs:double">
>    <xsl:param name="elt" as="element(*)" />
>    <xsl:value-of select="number(replace(local-name($elt), '^h(\d)$',
'$1'))"
> />
>  </xsl:function>
>
> </xsl:stylesheet>
> ===========8<-----------------------
>
> Gerrit

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.