[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Selective output of half a tag?

Subject: Re: Selective output of half a tag?
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Tue, 11 Jun 2002 04:20:46 -0700
peter a.tag
-----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">&lt;mytag></xsl:text>
</xsl:if>
<xsl:apply-templates/>
<xsl:if test="something-else">
  <xsl:text disable-output-escaping="yes">&lt;/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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.