title: Removing Parenthesis when linked at beginning and end of string xslt
body: <p>I am using XSLT to remove parenthesis from labels but only when they are at the beginning and end, and only if linked.</p>
  
  <p>For example</p>
  
  <p>(label (1)) becomes label (1)</p>
  
  <p>but</p>
  
  <p>(label) (1) remains (label) (1)</p>
  
  <hr>
  
  <p><strong>Edit for specifity</strong></p>
  
  <p>More examples:</p>
  
  <p>(1) becomes 1</p>
  
  <p>(This is)(a (label)) remains (This is)(a (label))</p>
  
  <p>(This (is) (a label)) becomes This (is) (a label)</p>
  
  <p>with whatever variation in between</p>
  
  <hr>
  
  <p>I thought I had a solution by testing characters in turn and keeping a counter to track if they were linked, but I am missing something still. Any assistance is appreciated.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xs" version="2.0"&gt;
  &lt;xsl:output indent="yes" method="xml" doctype-system="\\ukfs11\Bks_RAPS\XML\DTDs\TFB\TFB.dtd"/&gt;
  &lt;xsl:param name="labelstack"/&gt;
  &lt;xsl:param name="labeltest"/&gt;
  
  
  &lt;xsl:template match="//label[matches(.,'^\(.*\)$')]"&gt;
      &lt;xsl:param name="labelstack" select="substring(., 1, string-length(.) - 1)"/&gt;
      &lt;xsl:param name="labelcounter"/&gt;
      &lt;xsl:call-template name="loop1"&gt;
          &lt;xsl:with-param name="labelstack" select="$labelstack"/&gt;
          &lt;xsl:with-param name="labelcounter" select="0"/&gt;
      &lt;/xsl:call-template&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template name="loop1"&gt;
      &lt;xsl:param name="labeltest" select="substring($labelstack, 1,1)"/&gt;
      &lt;xsl:param name="labelstack" select="substring($labelstack, 2)"/&gt;
      &lt;xsl:param name="labelcounter"/&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="string-length($labeltest) = 0"&gt;
           &lt;xsl:copy&gt;
                  &lt;xsl:value-of select="substring(., 2, string-length(.) - 1)"/&gt;
           &lt;/xsl:copy&gt;
          &lt;/xsl:when&gt;
  
          &lt;xsl:when test="$labeltest = '('"&gt;
              &lt;xsl:variable name="labelcounter" select="$labelcounter + 1"/&gt;
              &lt;xsl:choose&gt;
  
                  &lt;xsl:when test="$labelcounter != 0"&gt;
                      &lt;xsl:call-template name="loop1"&gt;
                          &lt;xsl:with-param name="labelcounter" select="$labelcounter"/&gt;
                          &lt;xsl:with-param name="labelstack" select="$labelstack"/&gt;
                      &lt;/xsl:call-template&gt;
                  &lt;/xsl:when&gt;
  
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:copy&gt;
                          &lt;xsl:apply-templates select="node()|@*"/&gt;
                      &lt;/xsl:copy&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:when&gt;
  
          &lt;xsl:when test="$labeltest = ')'"&gt;
              &lt;xsl:variable name="labelcounter" select="$labelcounter - 1"/&gt;
              &lt;xsl:choose&gt;
  
                  &lt;xsl:when test="$labelcounter != 0"&gt;
                      &lt;xsl:call-template name="loop1"&gt;
                          &lt;xsl:with-param name="labelcounter" select="$labelcounter"/&gt;
                          &lt;xsl:with-param name="labelstack" select="$labelstack"/&gt;
                      &lt;/xsl:call-template&gt;
                  &lt;/xsl:when&gt;
  
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:copy&gt;
                          &lt;xsl:apply-templates select="node()|@*"/&gt;
                      &lt;/xsl:copy&gt;
                  &lt;/xsl:otherwise&gt;
  
              &lt;/xsl:choose&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
          &lt;xsl:call-template name="loop1"&gt;
                          &lt;xsl:with-param name="labelcounter" select="$labelcounter"/&gt;
                          &lt;xsl:with-param name="labelstack" select="$labelstack"/&gt;
                      &lt;/xsl:call-template&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="node()|@*"&gt;
  &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select="node()|@*"/&gt;
  &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>