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

Open Source alternatives to Adobe Creative Suite

With the take over of Macromedia by Adobe, they have increased their arsenal of design packages, as well as the price. No-one can do pretty much without the popular package of Photoshop for their images, and Dreamweaver for websites, especially aspiring design students who do not have much money. Nevertheless, open source software has come to save the day. What is open source software? In a nutshell it is free software with its source-code freely available to those who would like to expand or improve on the software. To expand further, anyone who’s tinkered with the source code such as optimisation or additional functionality can resubmit it back to the holders and let others enjoy the fruits of your labour. I have trawled through the World Wide Web to find out how everyone can benefit from Open Source to build up their own studio and compete against Adobe Creative Suite. All this for free, apart from the cost of the Internet. I am planning to have a facility where you can purchase...

Microsoft Bing

It's nice to see the Press taking a much more keen interest in technology and gadgets. So I wasn't that much surprised when the Metro covered the launch of Microsoft Bling. Currently in beta Microsoft is obviously renewing their competition against Google. I've also noticed that Microsoft has recently replaced the Live search with Bing on their MSN page. But after using it, why do I get the impression that it's just copying Google with a more colourful interface?

Javascript form validation

One of the most popular uses of Javascript is when it comes to validating user input on a form. The most hateful way, though, is by not using onsubmit. A number of times I come across code where someone uses an image in place of the submit button and uses the onclick event which will validate then submit the form. Yeauch! Very bad practice! The first thing to do with the form is place the Javascript function in the onsubmit attribute for the form, e.g. <form name="reg" method="post" onsubmit="return validateForm();" The use of return lets the form submit when the function returns true. If the function does not return true (i.e. false) then the form will not submit - as shown below. function validateForm(){ return false; } Getting a reference to the form is the next step. There are a number of ways to do this: document.forms[0] document.forms['reg'] document.getElementById('reg') This will work when id is used on the form Passing an o...