Apr 08
Mon
21

XSL Variable Quirks

Posted at 12:52 pm in Development

I'm still working on our Google Checkout integration at work. I have the web service done, but now I need to process the notifications from Google and update our database accordingly. This morning I was working on translating the XML from Google's 'New Order Notification' into a simpler XML that I can use to insert the order into our database by translating it through an XSL stylesheet. While doing so, I ran into a little quirk in how XSL translations are done.

I was splitting the name into pieces for first name, middle initial, and last name. While going through the various ways the name could be split, I was setting variables with the values. After all of the processing, I was using the variables to set the XML pieces that I needed. But I was getting errors saying that a couple of my variables weren't registered.

So I did some searching to find out what was up since I don't have a lot of experience with XSL (hence the issue I ran into). Apparently variables have VERY LIMITED scope. I was setting the variables with conditional statements inside of a template that was passed the name node to split.

CODE:
  1. <xsl:variable name="ContactFirstName" select="substring-before($name, ' ')" />
  2. <xsl:variable name="rest" select="substring-after($name, ' ')" />
  3. <xsl:choose>
  4.     <xsl:when test='contains($rest, ' ')'>
  5.         <xsl:variable name="ContactMiddleInitial" select="substring-before($rest, ' ')" />
  6.         <xsl:variable name="ContactLastName" select="substring-after($rest, ' ')" />
  7.     </xsl:when>
  8.     <xsl:otherwise>
  9.         <xsl:variable name="ContactMiddleInitial" select="''" />
  10.         <xsl:variable name="ContactLastName" select="$rest" />
  11.     </xsl:otherwise>
  12. </xsl:choose>
  13. <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  14. <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  15. <LastName><xsl:value-of select="$ContactLastName" /></LastName>

Doing the above code, I got errors that the ContactMiddleInitial and ContactLastName variables weren't registered. I was confused because they were set no matter what from the conditions. But apparently the xsl:variable in each case had a parent of either xsl:when or xsl:otherwise, and they weren't available outside of that. I didn't realize that.

So I changed my code to the following and all was fine:

CODE:
  1. <xsl:variable name="ContactFirstName" select="substring-before($name, ' ')" />
  2. <xsl:variable name="rest" select="substring-after($name, ' ')" />
  3. <xsl:choose>
  4.     <xsl:when test='contains($rest, ' ')'>
  5.         <xsl:variable name="ContactMiddleInitial" select="substring-before($rest, ' ')" />
  6.         <xsl:variable name="ContactLastName" select="substring-after($rest, ' ')" />
  7.         <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  8.         <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  9.         <LastName><xsl:value-of select="$ContactLastName" /></LastName>
  10.     </xsl:when>
  11.     <xsl:otherwise>
  12.         <xsl:variable name="ContactMiddleInitial" select="''" />
  13.         <xsl:variable name="ContactLastName" select="$rest" />
  14.         <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  15.         <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  16.         <LastName><xsl:value-of select="$ContactLastName" /></LastName>
  17.     </xsl:otherwise>
  18. </xsl:choose>

Seems kinda silly to me that you can't do what I tried in the first case. But I guess that's how the language is set up. The first question on this page has more detailed info on this: Q. Can I change the value of an XSLT variable?

This is mostly for my benefit in case I run into this issue again. But maybe it will help someone else out there. See ... I can write tech-related posts.

Leave a Reply

:rain: :blush2: :zzz2: :blush: :shock: :huh: :party: :doh: :) :winky: :frown: :clap: :wave: :think: :biglol: :whew: :pbbt: :sick: :p :storm: :angel: :drinkbeer: :hand: :question: :cowboy: :kiss: :banghead: :grin: :dance: :edit: :wha: :pissed: :S :2cents: :mad: :| B) :rolleyes2: >:) :giggle: :zzz1: :loser: :nailbite: :roflol: :cool: :D :arrow: :soapbox: :rofl: :bateyes: :razz1: :cry: :ligntning: :wink: :hey: :o :shh: :footmouth: :( :censored: :cry2: :rolleyes: :hugs: :drool: :crossfingers: :exclamation: :biggrin: :nahnah: :pin: :love: :beanie: :razz2: :crazy: :idea: :lol: :fullbeer: :thud:

I'm a Redneck Woman
Archives

Search
 
I'm a High Tech Broad
Stats
491 Posts & 80 Comments
Since 07 January 2007

49 queries in 0.836 seconds.
Feeds
Advertising
Meta
BlogRoll
Other Stuff
50% High Tech  50% Redneck
100% Woman
Valid CSS  Valid XHTML
This site looks best in Firefox  This site looks best at 1024x768 or higher
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Where You Can Find Me
twitter   flickr   delicious   brightkite   facebook   friendfeed
infield parking   myspace   jaiku   pownce   dopplr
Twitter
  • Sucky part of working out when I get home from work: I don't get to eat supper 'til almost 7pm. 1 hr ago
  • Damn. That 20 min today was a LOT harder than it was yesterday. But I did it. gotta get that routine started. I'm proud of me. :D 2 hrs ago
  • Ugh. Why am I so darn tired? I went to bed early last nite. I wish I could take a nap. And it's not even 1pm yet. 7 hrs ago
  • More updates...

Powered by Twitter Tools.

TwitterCounter for @hightechredneck
Affiliates
HostGator.com Affilliate

Get Smart. Get FireFox!

Get Thunderbird!
Other Cool Sites
kirtsy!
Tag Cloud
Recent TwitPic Uploads