Re: [xsl] node traversal

Subject: Re: [xsl] node traversal
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 24 Feb 2004 11:12:18 -0500
At 2004-02-24 10:06 -0500, Matthew L. Avizinis wrote:
I have a structure that _could_ be something like the following:
...
and I have to get to the following:
...
The idea is that any <p> elements remain unchanged, groups of sequential <inline> and text nodes are grouped together in a <p> element and any other elements, e.g. <table>, <math:math>, etc., are each wrapped with a <p> element.

This can be done in part with modes ... and I've given a start below but I've been called away to a meeting and I cannot finish ... the only requirement I haven't met yet is the grouping of inlines and sibling text nodes into a single <p> node ... that's heavy lifting and I've run out of volunteer list time for today and signing off the lists for the rest of the day.


I hope the example below gets you started ... sorry I can't finish it today.

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

T:\ftemp>type matthew.xml
<stem xmlns:math="math">
<table font-size="7">
.....more tags.....
</table>
<inline font-weight="bold" font-style="italic">Managerial accounting</inline>
differs from financial accounting in that financial accounting is
<p indent="15" left-indent="8">This is a test. This is a test of the emergency everything system.</p>
<p indent="10" left-indent="5">This is a second test. This is a test of the emergency everything system.</p>
<math:math width="100" height="18" id="test01">
......more tags......
</math:math>
there could be another text node here, for example
<math:math width="50" height="15" id="test02">
.......more tags.....
</math:math>
</stem>


T:\ftemp>type matthew.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="stem">
  <table-cell>
    <xsl:apply-templates mode="wrap"/>
  </table-cell>
</xsl:template>

<xsl:template match="p" mode="wrap"><!--just pass p's through-->
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="*|text()" mode="wrap">
  <p><xsl:apply-templates select="."/></p>
</xsl:template>

<!--ignore empty text nodes when wrapping-->
<xsl:template match="text()[not(normalize-space())]" mode="wrap"/>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
                                         <!--this cleans up the output-->
<xsl:template match="text()[not(normalize-space())]"/>

</xsl:stylesheet>

T:\ftemp>saxon matthew.xml matthew.xsl
<?xml version="1.0" encoding="utf-8"?>
<table-cell>
<p>
<table xmlns:math="math" font-size="7">
.....more tags.....
</table>
</p>
<p>
<inline xmlns:math="math" font-weight="bold" font-style="italic">Managerial accounting</inline>
</p>
<p>
differs from financial accounting in that financial accounting is
</p>
<p>This is a test. This is a test of the emergency everything system.</p>
<p>This is a second test. This is a test of the emergency everything system.</p>
<p>
<math:math xmlns:math="math" width="100" height="18" id="test01">
......more tags......
</math:math>
</p>
<p>
there could be another text node here, for example
</p>
<p>
<math:math xmlns:math="math" width="50" height="15" id="test02">
.......more tags.....
</math:math>
</p>
</table-cell>
T:\ftemp>rem Done



-- Public courses: Spring 2004 world tour of hands-on XSL instruction Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO United States: Washington, DC March 15; San Francisco, CA March 22 Finland April 26; Hong Kong May 17; Germany May 24; London June 07 World-wide on-site corporate, government & user group XML training

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)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


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



Current Thread