conditionals & attributes

Subject: conditionals & attributes
From: "Lawton, Scott" <slawton@xxxxxxxxxxxx>
Date: Fri, 6 Nov 1998 11:56:48 -0500
Many attributes in an arbitrary XML file are optional.  If present, I'd like
my XSL to do something with them.  If absent, leave them out (or perhaps add
some default value).

For example, if there's a "target" attribute, I want to output:
	<LABEL for="{attribute(target)}">
else:
	<LABEL>

Naturally the following gives an error since it's not well-formed:

	<xsl:choose>
		<xsl:when test="attribute(target)">
			<LABEL for="{attribute(target)}">
			</xsl:when>
		<xsl:otherwise>
			<LABEL>
			</xsl:otherwise>
		</xsl:choose>

	<!-- lots of stuff here that is common to both cases -->

	</LABEL>

I suppose the answer in this case is to put the common stuff in a macro --
assuming that macro invocation can be nested.  (The above snippet is from a
macro already.)

But maybe there's a more elegant solution, especially for multiple
attributes?  How about an option to exclude the attribute if the value is
empty?  Then I could just do:
	<LABEL for="{attribute(target)}" two="{attribute(foo)}"
three="{attribute(bar)}">
and would get a different result depending on which attributes had values.

Or maybe there's a way to build an output tag piecemeal, e.g.
	<xsl:insertTag name="LABEL">
		<xsl:when test="attribute(target)">
			for="{attribute(target)}"
			</xsl:when>
		<xsl:when test="attribute(foo)">
			two="{attribute(foo)}"
			</xsl:when>
		<xsl:when test="attribute(bar)">
			three="{attribute(bar)}"
			</xsl:when>
		</xsl:insertTag>

thoughts?

Scott

P.S. I realize the indendation is somewhat non-traditional, but it makes
sense if you use an outliner for editing....


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


Current Thread