[xsl] Wrapping pieces of content separately

Subject: [xsl] Wrapping pieces of content separately
From: <Emily.Garrett@xxxxxxxxxxx>
Date: Fri, 25 Aug 2006 11:15:36 -0400
Hi, Jay,
I can't thank you enough for your solution. It works like a charm. I
have been wracking my brain for days on this.

Emily

To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Subject: Re: [xsl] Wrapping pieces of content separately
Message-ID: <000d01c6c7c1$5bf9a220$301d7446@jayb>

Hi, Emily,

As it happens, I've solved this problem in the past. The trick to is
processing text nodes according to their context. If a p element has
children other than text nodes, then you don't want that p element to be
a p
element; you want it to be a series of elements. If a p element has just
text nodes (which really means just one text node, but that doesn't
matter),
then it should end up in a p element

The way to do it is to catch the text nodes of p elements that have
non-text-node children and wrap those text nodes in p elements. I've
done
that in the following stylsheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="doc">
    <out>
      <xsl:apply-templates/>
    </out>
  </xsl:template>

  <xsl:template match="p[*]">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="p">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="text()[parent::p[*]]">
    <p><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="ul">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="r">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="table">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="li">
    <p><xsl:apply-templates/></p>
  </xsl:template>

</xsl:stylesheet>

I got the desired output when I applied this stylesheet to your input
(after
I corrected it to have a document element and a closing tag for the ul
element).

HTH

Jay Bryant
Bryant Communication Services

Emily Garrett
Manager Production Tool Development
Thomson Global Production & Manufacturing
(513) 229-1526
emily.garrett@xxxxxxxxxxx

Your past is not your potential.  In any hour you can choose to liberate
the future.

--Marilyn Ferguson

Current Thread