Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

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

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:

  1. 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.
  2. Then, type the expression which contains the source string.
  3. Next, type , c, where c is the character after or before the substring that will be extracted.
  4. Finally, type ) to complete the function.
  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus