[xsl] Mixed Content to flat, grouping query

Subject: [xsl] Mixed Content to flat, grouping query
From: <chris.cole@xxxxxxxxxxx>
Date: Mon, 21 Feb 2005 23:01:53 -0000
I have mixed content that I need to change to being all child elements, so
instead of:

<document> blah
<body> blah
<content>The primary contact is <variable name="fred" /> as referred to in
<link>section 2</link> of this document.</content>
<content>The cat sat on the mat.</content>

<document> blah
<body> blah
<content><text>The primary contact is </text><variable name="fred"/>
<text> as referred to in </text><link>section 2</link><text> of this
<content><text>The cat sat on the mat.</text></content>

I thought I'd solved this problem using the XSL FAQ item on Grouping Variants
(thanks), but I could only achieve my desired result while testing the data
with the <subsection> as the ROOT element, but this fragment is a long way
down the document tree, and when I tried to copy the rest of the higher level
hierarchy etc it all went wrong.

Can someone help me copy the rest of my document as it is and only change this
low-level mixed content please?

I'm sure it's not difficult, but i can't see the wood for the trees anymore.
My current stylesheet is below.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
<xsl:output method="xml" />

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

<xsl:template match="content">
      <xsl:apply-templates select="node()[1]" />

<xsl:template match="link|variable">
   <xsl:copy-of select="." />
   <xsl:apply-templates select="following-sibling::node()[1]" />

<xsl:template match="*|text()">
      <xsl:apply-templates select="." mode="copy" />
   <xsl:apply-templates select="following-sibling::*[self::variable or
self::link][1]" />

<xsl:template match="*|text()" mode="copy">
   <xsl:copy-of select="." />
   <xsl:if test="not(following-sibling::node()[1]
   [self::variable or self::link])">
      <xsl:apply-templates select="following-sibling::node()[1]" mode="copy"

Many thanks,

Current Thread