Subject: Re: [xsl] Grouping a sequence of elements in different ways in the same stylesheet From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx> Date: Fri, 21 May 2010 23:50:43 +0200 |
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>
===========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<-----------------------
===========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: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:stylesheet> ===========8<-----------------------
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Grouping a sequence of el, Michael Kay | Thread | Re: [xsl] Grouping a sequence of el, Peter Desjardins |
RE: [xsl] Grouping a sequence of el, Michael Kay | Date | [xsl] Two recommendations: (1) Call, Costello, Roger L. |
Month |