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

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

Subject: RE: How to do an 'existence' test in XSL?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 22 Dec 2004 15:50:26 -0500
xsl variable existence
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[@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="{@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@xxxxxxxxxxxxxxxx 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 ======================================================================

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.