Re: Skipping surounding element

Subject: Re: Skipping surounding element
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 6 Nov 2000 18:55:21 +0000
Goetz,

> For all my 12 elements, I've a complex transformation each, that oututs
> exactly the same content for both cased, but if attr2 exists, the
> surrounding container is needed. For now I need two transformations for
> each element, is there a way to get rid of this?

There are a couple of ways to stop having to repeat code in the
stylesheet, which I think is what you're after.

One way is to store the result of transforming the content in a
variable, and then copy the content of the variable either with the
surrounding container, or without it. Thus the code for generating the
content is only involved once.  For example:

<xsl:variable name="content">
  <dest1>
    <xsl:if test="not(@attr2)">
      <xsl:copy-of select="@attr1" />
    </xsl:if>
    <xsl:apply-templates />
  </dest1>
</xsl:variable>
<xsl:choose>
  <xsl:when test="@attr2">
    <destC attr1="{@attr1}">
      <xsl:copy-of select="$content" />
    </destC>
  </xsl:when>
  <xsl:otherwise><xsl:copy-of select="$content" /></xsl:otherwise>
</xsl:choose>

The other way is to have the content be generated through the
application of a template in a particular mode:

<xsl:choose>
  <xsl:when test="@attr2">
    <destC attr1="{@attr1}">
      <xsl:apply-templates select="." mode="content" />
    </destC>
  </xsl:when>
  <xsl:otherwise>
    <xsl:apply-templates select="." mode="content" />
  </xsl:otherwise>
</xsl:choose>

...

<xsl:template match="*" mode="content">
  <dest1>
    <xsl:if test="not(@attr2)">
      <xsl:copy-of select="@attr1" />
    </xsl:if>
    <xsl:apply-templates />
  </dest1>
</xsl:template>

I hope that this helps,

Jeni

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



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


Current Thread