title: How to test for existence of a node whose path is given in a variable
body: <p>I am new to XSLT and not yet at ease with its syntax.</p>
  
  <p>I have a variable (name="pageName") that contains an xpath to a page element in the xml source tree. For example:</p>
  
  <blockquote>
    <p>/data/full-tree/page[@handle='en']/page[@handle='learn']/page[@handle='tutorials']</p>
  </blockquote>
  
  <p>Before displaying a link to this page in the html page, I would like to be sure that the page element exist thus I write the following <em>if</em> test:</p>
  
  <pre><code>&lt;xsl:if test="$pageName" &gt;
      &lt;a&gt;
          &lt;xsl:attribute name="href" &gt;
              &lt;xsl:value-of select='$theLink' /&gt;
          &lt;/xsl:attribute&gt;
          Click here
      &lt;/a&gt;
  &lt;/xsl:if&gt;
  </code></pre>
  
  <p>but it doesn't work as expected as the test result is always true even if the page element pointed by the xpath is not present in the tree.
  On the other hand, when I write the content of the variable directly into the test variable like this:</p>
  
  <pre><code> &lt;xsl:if test="/data/full-tree/page[@handle='en']/page[@handle='learn']/page[@handle='tutorials']
      &lt;a&gt;
          &lt;xsl:attribute name="href" &gt;
              &lt;xsl:value-of select='$theLink' /&gt;
          &lt;/xsl:attribute&gt;
          Click here
      &lt;/a&gt;
  &lt;/xsl:if&gt;   
  </code></pre>
  
  <p>it seems to work and display the link when the page element exists but not when it the page element doesn't exist.</p>
  
  <p><strong>My question is how could I use my variable properly?</strong></p>
  
  <p>Edit</p>
  
  <p>I will try to make my context more detailed</p>
  
  <p>The xml source tree (that represents the structure of a web site is like this</p>
  
  <pre><code>&lt;data&gt;
  &lt;full-tree&gt;
      &lt;page handle="root" id="12"&gt;
          &lt;name&gt;Root Page&lt;/name&gt;
          &lt;types&gt;
              &lt;type&gt;index&lt;/type&gt;
          &lt;/types&gt;
      &lt;/page&gt;
  
      &lt;page handle="en" id="1"&gt;
          &lt;name&gt;EN Root&lt;/name&gt;
          &lt;types&gt;
              &lt;type&gt;en&lt;/type&gt;
          &lt;/types&gt;
          &lt;page handle="home" id="3"&gt;
              &lt;name&gt;Home&lt;/name&gt;
              &lt;types&gt;
                  &lt;type&gt;en&lt;/type&gt;
              &lt;/types&gt;
          &lt;/page&gt;
          &lt;page handle="about" id="7"&gt;
              &lt;name&gt;About&lt;/name&gt;
              &lt;types&gt;
                  &lt;type&gt;en&lt;/type&gt;
              &lt;/types&gt;
              &lt;page handle="about-zoraldia" id="8"&gt;
                  &lt;name&gt;About zoraldia&lt;/name&gt;
                  &lt;types&gt;
                      &lt;type&gt;en&lt;/type&gt;
                  &lt;/types&gt;
              &lt;/page&gt;
          &lt;/page&gt;
          &lt;page handle="learn" id="21"&gt;
              &lt;name&gt;Learn&lt;/name&gt;
              &lt;types&gt;
                  &lt;type&gt;en&lt;/type&gt;
              &lt;/types&gt;
              &lt;page handle="tutorials" id="22"&gt;
                  &lt;name&gt;Tutorials&lt;/name&gt;
                  &lt;types&gt;
                      &lt;type&gt;en&lt;/type&gt;
                  &lt;/types&gt;
                  &lt;page handle="symphony" id="24"&gt;
                      &lt;name&gt;Symphony&lt;/name&gt;
                      &lt;types&gt;
                          &lt;type&gt;en&lt;/type&gt;
                      &lt;/types&gt;
                      &lt;page handle="create-site" id="23"&gt;
                          &lt;name&gt;Create a bilingual Web Site with the Symphony CMS&lt;/name&gt;
                          &lt;types&gt;
                              &lt;type&gt;en&lt;/type&gt;
                              &lt;type&gt;web-site&lt;/type&gt;
                          &lt;/types&gt;
                      &lt;/page&gt;
                      &lt;page handle="symphony-menu-page" id="48"&gt;
                          &lt;name&gt;Add level 3 and 4 Menu Pages&lt;/name&gt;
                          &lt;types&gt;
                              &lt;type&gt;en&lt;/type&gt;
                              &lt;type&gt;symphony&lt;/type&gt;
                          &lt;/types&gt;
                      &lt;/page&gt;
                  &lt;/page&gt;
              &lt;/page&gt;
          &lt;/page&gt;
          &lt;page handle="think" id="49"&gt;
              &lt;name&gt;Think&lt;/name&gt;
              &lt;types&gt;
                  &lt;type&gt;en&lt;/type&gt;
              &lt;/types&gt;
              &lt;page handle="free-software" id="50"&gt;
                  &lt;name&gt;Free Software&lt;/name&gt;
                  &lt;types&gt;
                      &lt;type&gt;en&lt;/type&gt;
                  &lt;/types&gt;
                  &lt;page handle="miscellaneous" id="51"&gt;
                      &lt;name&gt;Miscellaneous Articles&lt;/name&gt;
                      &lt;types&gt;
                          &lt;type&gt;en&lt;/type&gt;
                      &lt;/types&gt;
                      &lt;page handle="philo" id="52"&gt;
                          &lt;name&gt;Philosophy and Values of Free Software&lt;/name&gt;
                          &lt;types&gt;
                              &lt;type&gt;en&lt;/type&gt;
                          &lt;/types&gt;
                      &lt;/page&gt;
                  &lt;/page&gt;
              &lt;/page&gt;
          &lt;/page&gt;
      &lt;/page&gt;
      &lt;page handle="maintenance" id="9"&gt;
          &lt;name&gt;Maintenance notification&lt;/name&gt;
          &lt;types&gt;
              &lt;type&gt;maintenance&lt;/type&gt;
          &lt;/types&gt;
      &lt;/page&gt;
  &lt;/full-tree&gt;
  &lt;data&gt;
  </code></pre>
  
  <p>This represents English pages but there are also a pending structure in French.
  What I want to do is when the visitor is on a French page, display an EN link only if the page has been translated. </p>
  
  <p>Thus I use the French path (for example /fr/learn/tutorials and just exchange fr with en getting thus the /en/learn/tutorials into a $enLink variable.</p>
  
  <p>My goal is to display this link that works properly only if the page exist.</p>
  
  <p>I use a variable whose definition is</p>
  
  <pre><code>    &lt;xsl:variable name="pending"&gt;
               &lt;xsl:call-template name="build-xpath"&gt;
                 &lt;xsl:with-param name="output-string" select="'/data/full-tree'" /&gt;
                 &lt;xsl:with-param name="input-string" select="substring-after($enLink,'/')" /&gt;
             &lt;/xsl:call-template&gt;
       &lt;/xsl:variable&gt;
  </code></pre>
  
  <p>The build-xpath algorithm is the following:</p>
  
  <pre><code>&lt;xsl:template name="build-xpath" &gt;
      &lt;xsl:param name="output-string" /&gt;
      &lt;xsl:param name="input-string" /&gt;
  
       &lt;xsl:choose&gt;
           &lt;xsl:when test="contains($input-string, '/')" &gt;
  
              &lt;xsl:variable name="new-output"&gt;
                 &lt;xsl:value-of select="$output-string" /&gt;/page[@handle='&lt;xsl:value-of select="substring-before($input-string,'/')"/&gt;']
              &lt;/xsl:variable&gt;
  
              &lt;xsl:variable name="new-input" &gt;
                  &lt;xsl:value-of select="substring-after($input-string,'/')" /&gt;
              &lt;/xsl:variable &gt;
  
              &lt;xsl:call-template name="build-xpath" &gt;
                 &lt;xsl:with-param name="output-string"  select="$new-output" /&gt;
                 &lt;xsl:with-param name="input-string" select="$new-input" /&gt;
              &lt;/xsl:call-template&gt;
  
  
           &lt;/xsl:when&gt;
  
  
           &lt;xsl:otherwise&gt;&lt;xsl:value-of select="$output-string"/&gt;/page[@handle='&lt;xsl:value-of select="$input-string"/&gt;']
           &lt;/xsl:otherwise&gt;
  
  
       &lt;/xsl:choose&gt;
  
  
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>This algorithm returns a <em>pending</em> variable (the one I called <em>pageName</em> at first)</p>
  
  <p>Displayed with </p>
  
  <pre><code>&lt;xsl:value-of select="$pending" /&gt;
  </code></pre>
  
  <p>I see the string</p>
  
  <blockquote>
    <p>/data/full-tree/page[@handle='en']/page[@handle='learn']/page[@handle='tutorials']</p>
  </blockquote>
  
  <p>that is what I expect from the algorithm. </p>
  
  <p>What puts me into trouble is that when I put this string into another variable, say <em>anotherVar</em> </p>
  
  <pre><code>&lt;xsl:value-of select="$anotherVariable" 
  </code></pre>
  
  <p>doesn't display the string but the content of the node,  in this case:</p>
  
  <blockquote>
    <p>Tutorials en</p>
  </blockquote>
  
  <p>This seems normal but why in the case of $pending it is the string itself that is displayed ?</p>