[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: Antw: Re: [xsl] flatten a hierachy and change positions


Subject: Re: Antw: Re: [xsl] flatten a hierachy and change positions
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 25 Sep 2003 12:43:53 +0100

Hi Elke,

> thanks, I tried your examples. Just one probably very simple
> question is left. What do I have to change to keep my content, not
> just the elements. I assume with this additional part I would get
> both, opening and closing tags wouldn't I?

I assume that the XML that you're using is data-oriented: that every
element either contains other elements or contains some text that you
want to keep.

In that case, you could use something like:

<xsl:template match="*">
  <xsl:apply-templates select="*" />
  <xsl:copy>
    <xsl:apply-templates select="text()" />
  </xsl:copy>
</xsl:template>

With this template, if an element just contains other elements then
those elements will be processed first, and then there will be an
empty copy of the element itself. If an element contains text, then
you will get a copy of the element with the text that it contains
within the copy.

Alternatively, you could have different templates for the elements
that you want to be treated differently. If you want an element to be
copied with all its content, then use something like:

<xsl:template match="u1">
  <xsl:copy-of select="." />
</xsl:template>

If you want an empty copy of the element to appear before its content,
then use:

<xsl:template match="g1">
  <xsl:copy />
  <xsl:apply-templates select="*" />
</xsl:template>

and so on.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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



Current Thread
Keywords
xml