[Home] [By Thread] [By Date] [Recent Entries]
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. 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
|

Cart



