Re: [xsl] Approach for wrapping elements

Subject: Re: [xsl] Approach for wrapping elements
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 07 Jan 2003 14:55:06 -0500
7At 2003-01-07 13:24 -0600, you wrote:
I have a document that is of this form:
...
The elements above or below the listitems may be different. A list may also appear more than once in a document. Can anyone provide a method for wrapping listitems as a group with <itemizedlist> to produce this result?

An example is below. I've turned on indentation that you may wish to turn off.


Note that I match those items that have immediate preceding sibling items and do nothing with them, because the first of all sets of adjacent sibling items handles all of the following adjacent ones.

I hope this helps.

.............. Ken

T:\ftemp>type pratt.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
<para>Paragraph text</para>
<listitem>1st item</listitem>
<listitem>2nd item</listitem>
<listitem>3rd item</listitem>
<para>More text</para>
<para>Paragraph text</para>
<listitem>1st item</listitem>
<listitem>2nd item</listitem>
<listitem>3rd item</listitem>
<para>More text</para>
<para>Paragraph text</para>
<listitem>1st item</listitem>
<listitem>2nd item</listitem>
<listitem>3rd item</listitem>
<para>More text</para>
</test>
T:\ftemp>type pratt.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="node()" name="copy-this">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="listitem[preceding-sibling::*[1][self::listitem]]"/>

<xsl:template match="listitem">
  <itemizedlist>
    <xsl:apply-templates mode="copy-adjacent" select="."/>
  </itemizedlist>
</xsl:template>

<xsl:template mode="copy-adjacent" match="listitem">
  <xsl:call-template name="copy-this"/>
  <xsl:apply-templates select="following-sibling::*[1][self::listitem]"
                       mode="copy-adjacent"/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon pratt.xml pratt.xsl
<?xml version="1.0" encoding="utf-8"?>
<test>

<para>Paragraph text</para>

   <itemizedlist>
      <listitem>1st item</listitem>
      <listitem>2nd item</listitem>
      <listitem>3rd item</listitem>
   </itemizedlist>



<para>More text</para>

<para>Paragraph text</para>

   <itemizedlist>
      <listitem>1st item</listitem>
      <listitem>2nd item</listitem>
      <listitem>3rd item</listitem>
   </itemizedlist>



<para>More text</para>

<para>Paragraph text</para>

   <itemizedlist>
      <listitem>1st item</listitem>
      <listitem>2nd item</listitem>
      <listitem>3rd item</listitem>
   </itemizedlist>



<para>More text</para>

</test>
T:\ftemp>rem Done!


-- Upcoming hands-on in-depth North America: February 3- 7,2003 XSLT/XPath and XSL-FO Europe: February 17-21,2003

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1              Practical Formatting Using XSL-FO
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread