Extracting Substrings
When processing XML for output, it’s often useful to dig into a string and take out only the bit that you need. In this example (Figure 4.16), I am breaking up the location element into city and country by using the comma that separates them.
Figure 4.16 The wonder images now display at half their normal size: 264 by 175 pixels.
x s l t
...
<h2>Overview</h2>
<table border="1"><tr><th>Wonder Name
</th><th>City</th><th>Country</th>
<th>Years<br />Standing</th>
<th>Height</th></tr>
...
<td valign="top">
<xsl:value-of select="
substring-before(location, ',')"/>
</td>
<td valign="top">
<xsl:value-of select="
substring-after(location, ',')"/>
</td>
...
Figure 4.17. In the XML document, the location element contains both the wonder’s city and country. Here, I am separating the location element into two separate outputs of city and country. I am using the string before the comma for city, and after the comma for country.
To extract a substring that comes before or after a particular character:
- Type substring-after( or substring-before(, depending on whether you want to extract the part of the string that comes before or after the character.
- Then, type the expression which contains the source string.
- Next, type , c, where c is the character after or before the substring that will be extracted.
- Finally, type ) to complete the function.
