Skip to main content

Test for spaces in text

When it comes to XSL you have to pay particular attention to the schema of the XML you are trying to make use of.  There are certain cases when you do not have control over the XML being produced that your XSL needs to be generic.

I am currently updating a Community Portal, which I developed in the past for my current company and have just discovered a silly human error.  Yes. I made a bad mistake.  The coding for this system assumed that there would be no spaces in a post code when trying to build a list of links.

The block was along the lines of:

<a target="_blank">
  <xsl:attribute name="href">
    <xsl:value-of select="@theLink" />
    <!-- Check length of string and attach post code with a space -->
  </xsl:attribute>
  <xsl:attribute name="title">
    <!-- Attach the title of the link for accessibility-->
  </xsl:attribute>
  <xsl:value-of select="@linkText" />
</a>


The block to attach the post code just checked the length of the post code, split it into two, then concatenate with a space.  This logic did not check for a space. So the following method needs to be used in a conditional:

test="contains(//ResultItem/UKAddressStructure/BS7666Address/PostCode, ' ')"

As you can see, the space character is denoted within the single quotes. So:

<xsl:choose>
  <!-- It already has a space so we don't need to format -->
  <xsl:when test="contains(//ResultItem/UKAddressStructure/BS7666Address/PostCode, ' ')">
    <xsl:value-of select="//ResultItem/UKAddressStructure/BS7666Address/PostCode" />
  </xsl:when>
  <!-- No spaces. So we need to split the post code and concatenate with a space -->
  <xsl:otherwise>
    <xsl:value-of select='substring(//ResultItem/UKAddressStructure/BS7666Address/PostCode,1,4)' />
    <xsl:text>%20</xsl:text>
    <xsl:value-of select='substring(//ResultItem/UKAddressStructure/BS7666Address/PostCode,5)' />
  </xsl:otherwise>
</xsl:choose>


This is much more cleaner, where the post code is used appropriately.  The sample code above is for the original post code strings with more than 6 characters, so there are further checks for different post code sizes.

Blogged with Flock

Comments

Popular posts from this blog

Get the most out of your browser

A whole year without a blog post! Now that I've changed job, I'm hoping that I can settle down and push more useful tips on my blog. So, to get back to form I have one about web browser productivity. Web browsers have come a long way since it's inception in the early days of the World Wide Web. But if you've just been using it to browse the Web, you are really missing out on a ton of stuff. Productivity is just the start of it. Let's take a look at some of the hidden gems that a browser has to offer. I will mostly be focusing on Chrome and Firefox, but a lot (if not all) the tips may apply to your favourite browser. Pin Tabs First up is the ability to pin a tab. You do this by right-clicking on a tab and selecting Pin Tab . Did you just see how it react? It resize and just shows the icon. If you have a lot of tabs this is a real space saver. Using Keywords Firefox has keyword bookmarking, which is an absolute essential for many. This allows you to call a

Will we ever get Dreamweaver on Linux?

I have been a keen user of Ubuntu for some time now, and have to admit that this distribution has improved by leaps and bounds. However, what I do honestly miss on windows is... Dreamweaver. Some will swear by it, some will swear at the mention of its name. But for me, it is a very powerful tool when used correctly. Such a very popular and powerful web design and development tool is available on Windows and Mac OS, but not Linux. If you would just consider, Adobe has already supported the platform with their Adobe Reader and Adobe Air. Why is Dreamweaver not available? It just seems that there's not such a huge call for it so they just don't see the need to put in an effort for another platform. One chap at Adobe suggested to use Dreameaver under wine , but it doesn't work so great. There is an alternative to wine which, according to a user on the forums, works flawlessly - Crossover . However, the former option is not free, so some may be dissuaded where they woul

Image links in IE8

While mocking up a design page for a colleague's website I came across a new browser behaviour in IE8. This was the first time I used IE8 as my main development browser, due to the fact that the PC was re-installed and high-priority updates were applied. Surprisingly Microsoft branded IE8 as a priority update. Anyway, back to what I was talking about, here's a sample: <a href="index.htm"><img src="logo2.jpg" alt="Logo" title="Logo" /></a> As you can tell, this is using an image for a link, where it behaves well in Chrome, Firefox and in the previous versions of IE (7 and older). But in IE8, this is not the case as the DOM does not treat the image as innerText, only as a child node. As a result, everytime I hover the mouse over this image, an extra gap would come from nowhere shifting any relatively positioned elements. To fix this I explicitly added a space (&nbsp;), so the line would look like: <a href="inde