[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
Hello,
I have mixed content that I need to change to being all child elements, so
instead of:

INPUT:
<document> blah
<body> blah
<section>blah
<subsection>
<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>
</subsection>

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

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";
version="1.0">
<xsl:output method="xml" />

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

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

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

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

<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"
/>
   </xsl:if>
</xsl:template>
</xsl:stylesheet>

Many thanks,
Chris

Current Thread