|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Skipping surounding element
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








