| Subject: [xsl] XSLT 2.0 Grouping in XSLT 1.0 (was TEI > XML) From: Daniel O'Donnell <daniel.odonnell@xxxxxxxx> Date: Wed, 13 Apr 2005 00:05:01 -0600 | 
<p>Text <quote> <p>quotation text</p> </quote> end text.</p>
<p>text <blockquote> <p>quotation text</p> </blockquote> end text.</p>
<p>text</p> <blockquote> <p>quotation text</p> </blockquote> <p>end text</p>
Source XML document:
<p>Do <em>not</em>: <ul> <li>talk,</li> <li>eat, or</li> <li>use your mobile telephone</li> </ul> while you are in the cinema.</p>
Desired output:
<p>Do <em>not</em>:*</p>* <ul> <li>talk,</li> <li>eat, or</li> <li>use your mobile telephone</li> </ul> *<p>*while you are in the cinema.</p>
Solution:
This requires creating a p element around the maximal sequence of sibling nodes that does not include a ul or ol element.
This can be done by using group-adjacent, with a grouping key that is true if the element is a ul or ol element, and false otherwise:
<xsl:template match="p">
<xsl:for-each-group select="node()" group-adjacent="self::ul or self::ol">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:copy-of select="current-group()"/> </xsl:when>
<xsl:otherwise>
<p>
<xsl:copy-of select="current-group()"/>
</p>
</xsl:otherwise> </xsl:choose>
</xsl:for-each-group>
</xsl:template>
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
  </xsl:variable>  <xsl:template match="/">
    <html>
      <head>
        <title>Quotes test</title>
      </head>
      <body>
        <xsl:apply-templates select="//body"/>
      </body>
    </html>
  </xsl:template>  <xsl:template match="body">
    <xsl:apply-templates/>
  </xsl:template>  <xsl:template match="quote[p]">
    <blockquote>
      <xsl:apply-templates/>
    </blockquote><xsl:value-of select="$newline"/>
  </xsl:template>  <xsl:template match="table">
    <table><xsl:value-of select="$newline"/>
      <xsl:apply-templates/>
    </table><xsl:value-of select="$newline"/>
  </xsl:template>  <xsl:template match="row">
    <tr><xsl:value-of select="$newline"/>
      <xsl:apply-templates/><xsl:value-of select="$newline"/>
    </tr><xsl:value-of select="$newline"/>
  </xsl:template>  <xsl:template match="cell">
    <td>
      <xsl:apply-templates/>
    </td>
  </xsl:template>  <xsl:template match="list">
    <xsl:choose>
      <xsl:when test="@rend='ordered'">
        <ol><xsl:value-of select="$newline"/>
          <xsl:apply-templates/>
        </ol><xsl:value-of select="$newline"/>
      </xsl:when>
      <xsl:otherwise>
        <ul><xsl:value-of select="$newline"/>
          <xsl:apply-templates/>
        </ul><xsl:value-of select="$newline"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>  <xsl:template match="item">
    <li>
      <xsl:apply-templates/>
    </li><xsl:value-of select="$newline"/>
  </xsl:template>  <xsl:template match="p">
    <xsl:for-each-group select="node()"
      group-adjacent="self::quote[p] or self::table or self::list">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:apply-templates select="current-group()"/>
        </xsl:when>
        <xsl:otherwise>
          <p>
            <xsl:copy-of select="current-group()"/>
          </p>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:template>Thanks again. -dan
-- Daniel Paul O'Donnell, PhD Associate Professor of English University of Lethbridge Lethbridge AB T1K 3M4 Tel. (403) 329-2377 Fax. (403) 382-7191 E-mail <daniel.odonnell@xxxxxxxx> Home Page <http://people.uleth.ca/~daniel.odonnell/> The Digital Medievalist Project: <http://www.digitalmedievalist.org/>
| Current Thread | 
|---|
| 
 | 
| <- Previous | Index | Next -> | 
|---|---|---|
| RE: [xsl] How to output trademark c, Maria Amuchastegui | Thread | RE: [xsl] XSLT 2.0 Grouping in XSLT, Michael Kay | 
| Re: [xsl] Extra newline remove on t, omprakash . v | Date | Re: [xsl] how to translate XML with, Magic Finger | 
| Month |