xsl-list
[Top] [All Lists]

RE: How to do an 'existence' test in XSL?

2004-12-22 13:50:26
Hi George,

At 03:20 PM 12/22/2004, you wrote:
In this case the OP was grasping for a solution to a *simple* requirement,
in his words.  It's poor form if XSL can only offer a complex solution when
there is a simple need.

When all the possible values are known ahead of time (as is most likely the
case here), then a complex grouping algorithm would certainly not be
acceptable if something simpler would also do the job.

I don't know which solution would provide the best performance in this
instance, but unless this is a particularly performance critical section of
the application, then simplicity should win over performance considerations
every time.

Well sure: agreed on all points. But don't leave long-term maintainability and usability out of the picture.

Maybe I wasn't paying attention (if so I apologize): but I didn't take it as given that the values were known ahead of time, or always would be. Perhaps hashing this out with the OP would have been a good idea (but then we couldn't have this meta-conversation).

Often, in fact (and I say this not for your benefit, as you appear to have thought this through, but for others following along), it can be a tricky judgement call whether to implement the more general solution, which is a bit harder, or just hard-code the particular solution you know will work in this particular instance. Sometimes it's not clear when a stylesheet is a one-off that will never be used again over another data set -- or what constraints over the input data can be assumed if it is not. In such cases, I'll usually prefer to err on the side of generality.

FWIW, there are also intermediate solutions. For example, your stylesheet could contain a lookup table of expected values, and your template can simply iterate over this list and write out the node when a member of the set represented by the current list item can be identified. This is hard-coded, like the solution you offered, but slightly easier to maintain and extend for new values.

input:
<gui type="alertBox">...</gui>
<gui type="tooltip">...</gui>
<gui type="help">...</gui>
<gui type="tooltip">...</gui>
<gui type="alertBox">...</gui>
<gui type="tooltip">...</gui>
<gui type="help">...</gui>

stylesheet

<xsl:variable name="gui-type-list">
  <gui type="alertBox"/>
  <gui type="tooltip"/>
  <gui type="alertbox"/>
</xsl:variable>

<xsl:variable name="gui-types"
  select="document('')/*/xsl:variable[(_at_)name='gui-type-list']/gui"/>

<xsl:variable name="all-guis" select="//gui"/>

and then, in a template,

<xsl:for-each select="$gui-types">
  <xsl:if test="@type = $all-guis/@type">
    <gui type="{(_at_)type}"/>
  </xsl:if>
</xsl:for-each>

This would run pretty fast. (You be the judge of how simple it is.)

In any case, thanks for the reminder that generality is not always the most important virtue in a stylesheet. I think the OP is getting his money's worth this time. :->

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--