Re: [xsl] Looking for a concise way of specifying an conditional attribute in output

Subject: Re: [xsl] Looking for a concise way of specifying an conditional attribute in output
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Thu, 5 May 2011 16:27:37 -0400
On this note, it seems to me that xsl:value-of is one of the most
misunderstood instructions for novices.  For that matter, even more
experienced users seem to use it in places where xsl:apply-templates,
xsl:copy-of or, in XSLT 2.0, xsl:sequence might be better choices.

I'd kind of like to see XSLT provide an alternative in the form of
allowing the "select" attribute on xsl:text with the same effect as on
xsl:value-of.  This would be a) slightly more concise, b) in line with
other node-type instructions, such as xsl:attribute, and c) more
descriptive (IMHO) of the result, in that "text" suggests that the
result will be a string, rather than the more ambiguous "value".  I
dare say XSLT might even consider deprecating xsl:value-of in favor of
this, for the relatively few (compared to how often it seems to be
used, today) cases where this particular functionality is needed.

-Brandon :)

On Thu, May 5, 2011 at 4:06 PM, John McGowan <john@xxxxxxxxxxxxx> wrote:
> Thank you Andrew, Michael and Ken!
> I use value-of so much I forgot about it's impact on "non-strings".
> This information also helped me with a very similar issue I just ran
> into today where I was writing a function that was returning a boolean
> true or false, but it was always true... because value-of was turning
> it into a string.
> /John
> On Thu, May 5, 2011 at 3:25 AM, Andrew Welch <andrew.j.welch@xxxxxxxxx>
>>> <option value="{@value}>
>>>  <xsl:value-of select="isSelected(@value,something)">
>>>  other stuff
>>> </option>
>>> with a reusable function like this
>>> <xsl:function name="isSelected">
>>>  <xsl:param name="v1"/>
>>>  <xsl:param name="v2"/>
>>>  <xsl:if test="$v1 eq $v2"><xsl:attribute name="selected"
>>> select="'selected'" /></xsl:if>
>>> </xsl:function>
>> As has been said you need xsl:sequence or xsl:copy-of to get the whole
>> node not just the value of the node, but you could also make that
>> function a little more generic to create any attribute:
>> <xsl:function name="f:createAtt">
>>  <xsl:param name="name"/>
>>  <xsl:param name="value"/>
>>  <xsl:attribute name="{$name}" select="$value"/>
>> </xsl:function>
>> and then call it passing the the name value pair for the attribute,
>> and put the condition in a predicate:
>> <foo>
>>  <xsl:sequence select="f:createAtt('foo', 'bar')[current()/@value =
>> $something]"/>
>> cheers
>> andrew
>> --
>> Andrew Welch
> --
> /John

Current Thread