title: Using XSLT to split a string
body: <p>I'm using XSLT to transform XML files into a format that Excel can delimit (sample code shown later). For example, when opened in Excel, the delimited version might look something like:</p>
  
  <pre><code>+---------------+---------------+----------+
  |URL            |Title          | Version  |
  +---------------+---------------+----------+
  |dogs_are_cool  |Dogs are cool  | May 2013 |
  +---------------+---------------+----------+
  </code></pre>
  
  <p>The problem is related to the fact that every URL has the version appended at the end. Using the previous example, <code>dogs_are_cool</code> is actually <code>dogs_are_cool_may2013.html</code>.</p>
  
  <p>I'd like to do two things with that appended version:</p>
  
  <ul>
  <li>Remove the version when printing the URL.</li>
  <li>Reformat and print the version.</li>
  </ul>
  
  <p>I'm guessing the best way to do this is by somehow splitting the URL on the underscores. Then putting the last element split in one variable and printing the other elements in order--inserting the underscores back in. </p>
  
  <p>I'm not sure how to go about that.</p>
  
  <p>Sample XML:</p>
  
  <pre><code>&lt;contents Url="toc_animals_may2013.html" Title="Animals"&gt;
      &lt;contents Url="toc_apes_may2013.html" Title="Apes"&gt;
          &lt;contents Url="chimps_may2013.html" Title="Some Stuff About Chimps" /&gt;
      &lt;/contents&gt;
      &lt;contents Url="toc_cats" Title="Cats"&gt;
          &lt;contents Url="hairless_cats_may2013.html" Title="OMG Where Did the Hair Go?"/&gt;
          &lt;contents Url="wild_cats_may2013.html" Title="These Things Frighten Me"/&gt;
      &lt;/contents&gt;
      &lt;contents Url="toc_dogs_may2013.html" Title="Dogs"&gt;
          &lt;contents Url="toc_snorty_dogs_may2013.html" Title="Snorty Dogs"&gt;
              &lt;contents Url="boston_terriers_may2013.html" Title="Boston Terriers" /&gt;
              &lt;contents Url="french_bull_dogs_may2013.html" Title="Frenchies" /&gt;
          &lt;/contents&gt;
      &lt;/contents&gt;
  &lt;/contents&gt;
  </code></pre>
  
  <p>Sample XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="text" indent="no"/&gt;
  
      &lt;!-- This variable sets the delimiter symbol that Excel will use to seperate the cells --&gt;
      &lt;xsl:variable name="delimiter"&gt;@&lt;/xsl:variable&gt;
  
      &lt;xsl:template match="contents"&gt;
  
          &lt;!-- Prints the URL --&gt;
          &lt;xsl:value-of select="@Url"/&gt;
          &lt;xsl:copy-of select="$delimiter" /&gt;
  
          &lt;!-- Prints the title --&gt;
          &lt;xsl:apply-templates select="@Title"/&gt;
          &lt;xsl:copy-of select="$delimiter" /&gt;
  
          &lt;!-- I'd like to print the version here --&gt;
          &lt;xsl:copy-of select="$delimiter" /&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:apply-templates select="//contents"/&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>