Re: [xsl] Wrapping nodes outside tables into paragraphs

Subject: Re: [xsl] Wrapping nodes outside tables into paragraphs
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 22 May 2014 19:54:43 -0000
On Thu, May 22, 2014 at 07:19:02PM -0000, Martynas JuseviD
ius martynas@xxxxxxxxxxxx scripsit:
> I have TEI markup where nodes (text nodes and some elements) are
> interspersed with table and figure elements, like this:
> 
>   <note>
>     text text <ref type="whatever">text</ref>
>     <table>...</table>
>     text text
>     <figure>...</figure>
>     text text
>     <table>...</table>
>     <table>...</table>
>     text text
>   </note>
> 
> There can be nodes before, between, and after tables.
> 
> I want to produce a corresponding XHTML while also wrapping all the
> nodes between tables into <p>, so they're on the same block-level (for
> simplicity, lets say <xhtml:img> is also block-level):

This is a classic use case for XSLT 2.0's for-each-group instruction.

Are you using 2.0?

If so, an approach like

<xsl:for-each-group select="note/node()" group-adjacent="if
(self::table) then 'table' else 'interstitial'">

    <xsl:choose>
        <xsl:when test="current-grouping-key() eq 'table'">
            <!-- do what you do to tables -->
        </xsl:when>
        <xsl:when test="current-grouping-key() eq 'interstial'>
            <!-- wrap everything else -->
        <xsl:otherwise>
            <!-- you should never get here; die loudly -->
        </xsl:otherwise>
    </xsl:choose>

</xsl:for-each-group>

ought to work.  (The pattern also extends.)

If you have to individual wrap adjacent tables you may want a different
grouping attribute, and you might want to select
note/node()[normalize-space()] instead, to avoid white-space only text
nodes.

-- Graydon

Current Thread