xsl-list
[Top] [All Lists]

Re: [xsl] Wrapping nodes outside tables into paragraphs

2014-05-22 14:54:55
On Thu, May 22, 2014 at 07:19:02PM -0000, Martynas Jusevičius 
martynas(_at_)graphity(_dot_)org 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
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

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