[Novalug] Macro preprocessing

Peter Larsen plarsen@famlarsen.homelinux.com
Fri Jun 25 13:33:54 EDT 2010


On Fri, 2010-06-18 at 20:44 -0700, Jim Ide wrote:
> 
> XML and XSLT come the closest to what I want, except that the
> input text must be valid XML, and the XSLT template syntax
> is cluttered, clunky, frustrating, and gives me a headache.
> It also does not support parameter type checking. 

I'll grab this one since it's sorta in the area I've been for about a
decade now. Quickly though, in regards to named parameters that's
solvable through overloading. I prefer to have procedures where I know
what the input/context is when I create them. Instead of a procedure
with 30 "potential" parameters that I have to work with not knowing the
context of which they're set in. In Java you'll simply overload your
method in the class which allows your IDE to show the programmer the
different expected way to call the same function. Languages like
Oracle's PL/SQL does what you want - but I wouldn't exactly call that a
programming language you can use outside the DB.

XML/XSLT is an interesting toolset. I've created full websites and quite
advanced features with that combination. It's a powerful set of tools
all based on pattern recognition - mostly XPATH based - and as you
pointed out, it's loosely coupled when it comes to types. That doesn't
mean it doesn't match on types - it does - but not in the way you expect
(XPATH expressions are type based). If you want the same functionality
as xslt, but with a tight type check you should look into XQuery. It was
widely favored by BEA WebLogic before they become part of Oracle - it
looks like that the language may have fallen out of favor for the lesser
coupled XSLT. It took me a long time to get used to XQuery but once I
did, it can do some neat stuff.

Not sure I would agree with you on the template part of XSLT of being
cluttered/clunky. It's pretty straight forward. It's read/processed
sequentially, so the first match wins (use "mode" to control context if
you don't want this feature). You actually DO pass parameters by name
with XSLT so that part should be easier for you.

Example:

<sample prefix="My name is">
  <data>Peter</data>
  <data>Hans</data>
</sample>

== eof ==

<? xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:out="http://nowhere.com/data">

<xsl:output format="xml" />

<xsl:template match="/">
   <out:data>
     <xsl:apply-templates match="data">
       <xsl:with-param name="text" select="@prefix" />
     </xsl:apply-templates>
   </out:data>
</xsl:template>

<xsl:template match="data">
  <xsl:param name="text" select="'default'" />
  <xsl:param name="notpassed" select="'and I know XSLT'" />
  <out:greeting>
    <xsl:value-of select="concat($text,' ')" />
    <xsl:value-of select="normalize-space(.)" />
    <xsl:value-of select="concat(' ',$notpassed)" />
  </out:greeting>
</xsl:template>

=== eof ====
When run, you get:
  <out:data xmlns:out="http://nowhere.com/data">
    <out:greeting>My name is Peter and I know XSLT</out:greeting>
    <out:greeting>My name is Hans and I know XSLT</out:greeting>
  </out:data>

This is a very rough example (not tested - hopefully I got all the typos
fixed). But you should see the basic features including calling with
named parameters. Not sure I would call it clunky - it's just XML and
the nature of XML. Maybe you could explain how you feel the above
example violates coding "etiquette" or how-ever you would put it.

With XQuery your input/output is validated against the XSD defining your
data set. This is a great help for trouble shooting both under
development and production problems. XSLT doesn't care. It just doesn't
find a match. However, XQuery doesn't have named parameter passing. 

The challenge with XSLT is XPath. You need to know the query language
quite well to make advanced code possible. Here's an example of some old
XSLT code of mine:

     <xsl:if test="position()=1 or
not(@level=preceding-sibling::step/@level)">
       <tr>
         <td colspan="3" class="header">
           <xsl:value-of select="@level"/>
         </td>
       </tr>       
     </xsl:if>

It'll take a while to understand what's really going on here. It's not
because of XSLT but because of the XPATH queries (this is part of
building up a menu make making "separation headers" between main and
sub-menues).

-- 
Best Regards
  Peter Larsen

Wise words of the day:
A black cat crossing your path signifies that the animal is going somewhere.
		-- Groucho Marx
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <https://lists.firemountain.net/pipermail/novalug/attachments/20100625/e1df7084/attachment.asc>


More information about the Novalug mailing list