xsl-list
[Top] [All Lists]

Re: [xsl] Grouping a sequence of elements in different ways in the same stylesheet

2010-05-24 10:54:10
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(_dot_)imsieke(_at_)le-tex(_dot_)de> 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[(_at_)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[(_at_)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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>