title: Hyperlink an XML child element with XPath and XSLT
body: <p>I have an XML file that stores movies and their actors.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="index.xsl"?&gt;
  &lt;movies
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="movies.xsd"&gt;
  
  &lt;movie movieID="1"&gt;
      &lt;actors&gt;
          &lt;actor actorID="1"&gt; 
              &lt;name link="bob.website.com"&gt;Bob&lt;/name&gt;
              &lt;age&gt;29&lt;/age&gt;
              &lt;height&gt;1.75 m&lt;/height&gt;
              &lt;from&gt;USA&lt;/from&gt;
          &lt;/actor&gt;
  
          &lt;actor actorID="2"&gt; 
              &lt;name link="jack.website.com"&gt;Jack&lt;/name&gt;
              &lt;age&gt;24&lt;/age&gt;
              &lt;height&gt;1.83 m&lt;/height&gt;
              &lt;from&gt;UK&lt;/from&gt;         
          &lt;/actor&gt;
  
          &lt;actor actorID="3"&gt; 
              &lt;name&gt;James&lt;/name&gt;  
          &lt;/actor&gt;
  
          &lt;actor actorID="4"&gt; 
              &lt;name&gt;Tom&lt;/name&gt;    
          &lt;/actor&gt;
  
          &lt;actor actorID="5"&gt; 
              &lt;name&gt;Mark&lt;/name&gt;   
          &lt;/actor&gt;
      &lt;/actors&gt;   
  &lt;/movie&gt;
  
  &lt;/movies&gt;
  </code></pre>
  
  <p>As you can see from the code above, I have 5 actors located inside the "movie" element. The first 2 actors contain 4 child elements
  (name, age, height and from) as well as the attribute "link" inside the "name" element that provides a hyperlink to the specific actor's page. The other 3 actors only contain the element "name".</p>
  
  <p>I would like my page to display 5 actors' names in a list with the first 2 names displayed as hyperlinks (link to the specific actor's page) and the other 3 to be displayed as regular text.</p>
  
  <p>The way I want to distinguish between a hyperlinked name and a regular name is through XPath.
  I want all "actor" elements that contain more than 1 child element (in this case name, age, height and from) to be displayed as hyperlinks. 
  All other "actor" elements containing no more than 1 child element to be displayed as regular text</p>
  
  <p>This is my XSLT file containing the Xpath query that I've written.</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="xml" version="1.0" omit-xml-declaration="yes" indent="yes" media-type="text/html"/&gt;
  
  &lt;xsl:template match="movie"&gt;    
      &lt;xsl:text&gt;Actors: &lt;/xsl:text&gt;
      &lt;xsl:apply-templates select="actors/actor"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="actor"&gt;
      &lt;xsl:if test="//actor/*[position()&gt;1]/../name"&gt;
          &lt;a href="{@link}"&gt;
              &lt;xsl:value-of select="name"/&gt;
          &lt;/a&gt;
      &lt;/xsl:if&gt;
      &lt;xsl:element name="br"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I have managed to write the query <code>//actor/*[position()&gt;1]/../name</code> and I do get only the first 2 actors's names displayed (tested in Xpath notepad++ plugin).
  I just don't know how to connect them with the "link" attribute properly and display them on the page.</p>
  
  <p>This is the end result that I want to achieve:</p>
  
  <p><img src="https://i.imgur.com/p8iBjzX.png?1" alt="actors image"></p>
  
  <p>The first 2 actors' names are hyperlinked because the "actor" elements contain MORE than 1 child element.
  The other 3 "actor" elements only contain 1 child element and therefore their corresponding "names" are displayed as plain text.</p>