This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Selective output of half a tag?
- From: Peter Davis <pdavis152 at attbi dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Tue, 11 Jun 2002 04:20:46 -0700
- Subject: Re: [xsl] Selective output of half a tag?
- References: <1023792614.16066.10.camel@walter>
- Reply-to: xsl-list at lists dot mulberrytech dot com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tuesday 11 June 2002 03:50, Graham Ashton wrote:
> <xsl:if test="self::foo">
> <mytag>
> </xsl:if>
> <xsl:apply-templates/>
> <xsl:if test="count(following-sibling::foo)=0">
> </mytag>
> </xsl:if>
Yes, that doesn't work because it is not well-formed.
Basically, there are three ways you can do this (depending a little on what
you're trying to do):
If all you need to do is dynamically change the name of the tag, then
<xsl:element> can help you out:
<xsl:element name="{$the-tag-name}">
<!-- assuming "the-tag-name" is a variable -->
<xsl:apply-templates/>
</xsl:element>
If you need to selectively output or not output the entire tag, then you need
something like this:
<xsl:choose>
<xsl:when test="something">
<mytag>
<xsl:apply-templates/>
</mytag>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
As you might notice, the above basically has two possible outputs, and each
choice has its own copy of <xsl:apply-templates/>. You've already done the
hard part of putting the common code in (an)other template(s). If what you
had was more than a single command inside of <mytag/>, then you would
probably want to move the common code to a named template and only copy the
<xsl:call-template/> inside <mytag/> and <xsl:otherwise/>.
The final and most despised option is disable-output-escaping. This is the
lazy way out, but it follows your idea of telling the processor to treat
"<mytag>" as text. You run two risks: (1) you might end up outputting
not-well-formed XML, and (2) this won't work with all processors or
installations.
<xsl:if test="something">
<xsl:text disable-output-escaping="yes"><mytag></xsl:text>
</xsl:if>
<xsl:apply-templates/>
<xsl:if test="something-else">
<xsl:text disable-output-escaping="yes"></mytag></xsl:text>
</xsl:if>
HTH
- --
Peter Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9Bd0RNSZCJx7tYycRAn+CAKCW5Ly+RIHWEv3qOL+v6ds5AxvCqACgog7K
MC2DatL184GQhcJBOk7fBQQ=
=IfUy
-----END PGP SIGNATURE-----
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list