RE: [xsl] creating container elements

Subject: RE: [xsl] creating container elements
From: "Andrew Welch" <AWelch@xxxxxxxxxxxxxxx>
Date: Tue, 6 Apr 2004 13:44:30 +0100
If you input is as follows:

<foo>
  <bar id="bar-1">
    <barfoo id="barfoo-1"/>
  </bar>
  <bar id="bar-2">
    <barfoo id="barfoo-2"/>
  </bar>
  <bar id="bar-3">
    <barfoo id="barfoo-3"/>
  </bar>
</foo>

And all you want to do is wrap all the <bar> elements in a single
<container> elements, you can use:

  <xsl:template match="foo">
    <foo>
      <container>
        <xsl:copy-of select="bar"/>
      </container>
    </foo>
  </xsl:template>

Note, you can create 'literal result elements' such as <container> by
writing them directly without using <xsl:element name="container">.

If your input is a little more complated, for example:

<foo>
  <apple/>
  <orange/>
  <apple/>
  <pear/>
  <apple/>
<foo>

An you would like to wrap all <apple>'s in a <container>, then process
any other elements you can use:

<xsl:template match="foo">
  <foo>
    <container>
      <xsl:copy-of select="apple"/>
    </container>
    <xsl:apply-templates select="*[not(self::apple)]"/>
  </foo>
</xsl:template>

You won't need to use modes here,

cheers
andrew

Current Thread