RE: [xsl] XSL generating PHP/MySQL

Subject: RE: [xsl] XSL generating PHP/MySQL
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Thu, 27 Sep 2007 16:12:46 -0500
Maybe I can. :P
(sorry, didn't mean for it to sound harsh.)

Riku -
  Here's a quick refactor, and some suggestions.

<xsl:choose>
	<xsl:when test="CASE1">mysql_query("SET
@index=LAST_INSERT_ID()");
	$index="@index";</xsl:when>
	<xsl:when test="CASE2">$index='20';</xsl:when>
	<xsl:otherwise>$index='0';</xsl:otherwise>
</xsl:choose>

<xsl:text/>mysql_query("replace into table (index, variable) values
(\"$index\", '<xsl:value-of select="$variable"/>') ");<xsl:text/>


Remember, when you're creating XSLT stylesheets, you're working with
XML, which will behave (more or less) as you'd expect it to. Whitespace
between nodes will only show up if it contains non-whitespace text data.
This can work for you or against you; in the case of your first
<xsl:when>, you'd want that newline and tab, but in the following
<xsl:text>, you wouldn't.

Remember, <xsl:choose> returns as soon as it encounters a true
condition. You don't need another <xsl:choose> in the <xsl:otherwise>
section; if CASE1 is true, CASE2 will never be evaluated.

Use <xsl:text> sparingly. If you can get rid of it altogether (in the
<xsl:when> sections, in particular), do so; it'll do wonders for your
readability and verbosity. Since <xsl:text> can't contain
<xsl:value-of>, if you need to replicate that behavior, wrap that
content in empty nodes - <xsl:text/>. Since the whitespace before the
first <xsl:text/> and after the second <xsl:text/> will be discarded,
you'll get behavior identical to what you were looking for before that.

Last, when choosing between <xsl:text> and <xsl:value-of>, take a look
at how much of the output data is plaintext, and how much needs to be
evaluated with <xsl:value-of>. In the case of $variable, that was the
only evaluated part of the entire string, and it was cleaner to pull
everything else out and leave $variable alone in there. Sometimes you'll
need to intersperse several XPath expressions or variables among several
small chunks of plaintext; in that case, it's usually cleaner to pull
all of it into one statement like this:

<xsl:value-of select="concat('plaintext', $var,
                             ', txt, ',   /expr/@attr, ', etc')/>


It's just code style at that point (a lot of the above is) but if it's
more readable, you'll be far less reticent to alter it later. And others
will be much more willing to help you if something goes wrong. :)

~ Scott


-----Original Message-----
From: Abel Braaksma [mailto:abel.online@xxxxxxxxx]
Sent: Thursday, September 27, 2007 3:44 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSL generating PHP/MySQL

Scott Trenda wrote:
> Also, your code hurts my eyes. :P
>

If you make a statement like that, it would be nice to follow up to the
OP on how to amend it so that your eyes aren't hurt that much anymore.
Nobody wants you to have to go to an optician, and the OP is probably
not able to prevent it... maybe you can help? :P

Cheers,
-- Abel Braaksma --

Current Thread