<p>Given the following XML:</p>

<pre><code>&lt;current&gt;
  &lt;login_name&gt;jd&lt;/login_name&gt;
&lt;/current&gt;
&lt;people&gt;
  &lt;person&gt;
    &lt;first&gt;John&lt;/first&gt;
    &lt;last&gt;Doe&lt;/last&gt;
    &lt;login_name&gt;jd&lt;/login_name&gt;
  &lt;/preson&gt;
  &lt;person&gt;
    &lt;first&gt;Pierre&lt;/first&gt;
    &lt;last&gt;Spring&lt;/last&gt;
    &lt;login_name&gt;ps&lt;/login_name&gt;
  &lt;/preson&gt;
&lt;/people&gt;
</code></pre>

<p>How can I get "John Doe" from within the current/login matcher?</p>

<p>I tried the following:</p>

<pre><code>&lt;xsl:template match="current/login_name"&gt;
  &lt;xsl:value-of select="../people/first[login_name = .]"/&gt;
  &lt;xsl:text&gt; &lt;/xsl:text&gt;
  &lt;xsl:value-of select="../people/last[login_name = .]"/&gt;
&lt;/xsl:template&gt;
</code></pre>

[{'answer_id': 62010, 'author': 'Kendall Helmstetter Gelner', 'author_id': 6330, 'author_profile': 'https://Stackoverflow.com/users/6330', 'pm_score': 0, 'selected': False, 'text': '<p>I think what he actually wanted was the replacement in the match for the "current" node, not a match in the person node:</p>\n\n<pre><code>&lt;xsl:variable name="login" select="//current/login_name/text()"/&gt;\n\n&lt;xsl:template match="current/login_name"&gt;\n&lt;xsl:value-of select=\'concat(../../people/person[login_name=$login]/first," ", ../../people/person[login_name=$login]/last)\'/&gt;\n\n&lt;/xsl:template&gt;\n</code></pre>\n'}, {'answer_id': 62011, 'author': 'jelovirt', 'author_id': 2679, 'author_profile': 'https://Stackoverflow.com/users/2679', 'pm_score': 2, 'selected': False, 'text': '<p>You want <a href="http://www.w3.org/TR/xslt#function-current" rel="nofollow noreferrer"><code>current()</code></a> function</p>\n\n<pre><code>&lt;xsl:template match="current/login_name"&gt;\n  &lt;xsl:value-of select="../../people/person[login_name = current()]/first"/&gt;\n  &lt;xsl:text&gt; &lt;/xsl:text&gt;\n  &lt;xsl:value-of select="../../people/person[login_name = current()]/last"/&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n\n<p>or a bit more cleaner:</p>\n\n<pre><code>&lt;xsl:template match="current/login_name"&gt;\n  &lt;xsl:for-each select="../../people/person[login_name = current()]"&gt;\n    &lt;xsl:value-of select="first"/&gt;\n    &lt;xsl:text&gt; &lt;/xsl:text&gt;\n    &lt;xsl:value-of select="last"/&gt;\n  &lt;/xsl:for-each&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n'}, {'answer_id': 62081, 'author': 'Matt Large', 'author_id': 2978, 'author_profile': 'https://Stackoverflow.com/users/2978', 'pm_score': 0, 'selected': False, 'text': '<p>Just to add my thoughts to the stack</p>\n\n<pre><code>&lt;xsl:template match="login_name[parent::current]"&gt;\n &lt;xsl:variable name="login" select="text()"/&gt;\n &lt;xsl:value-of select=\'concat(ancestor::people/child::person[login_name=$login]/child::first/text()," ",ancestor::people/child::person[login_name=$login]/child::last/text())\'/&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n\n<p>I always prefer to use the axes explicitly in my XPath, more verbose but clearer IMHO.</p>\n\n<p>Depending on how the rest of the XML documents looks (assuming this is just a fragment) you might need to constrain the reference to "ancestor::people" for example using "ancestor::people[1]" to constrain to the first people ancestor.</p>\n'}, {'answer_id': 62429, 'author': 'JeniT', 'author_id': 6739, 'author_profile': 'https://Stackoverflow.com/users/6739', 'pm_score': 4, 'selected': True, 'text': '<p>I\'d define a key to index the people:</p>\n\n<pre><code>&lt;xsl:key name="people" match="person" use="login_name" /&gt;\n</code></pre>\n\n<p>Using a key here simply keeps the code clean, but you might also find it helpful for efficiency if you\'re often having to retrieve the <code>&lt;person&gt;</code> elements based on their <code>&lt;login_name&gt;</code> child.</p>\n\n<p>I\'d have a template that returned the formatted name of a given <code>&lt;person&gt;</code>:</p>\n\n<pre><code>&lt;xsl:template match="person" mode="name"&gt;\n  &lt;xsl:value-of select="concat(first, \' \', last)" /&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n\n<p>And then I\'d do:</p>\n\n<pre><code>&lt;xsl:template match="current/login_name"&gt;\n  &lt;xsl:apply-templates select="key(\'people\', .)" mode="name" /&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n'}, {'answer_id': 77868, 'author': 'leekelleher', 'author_id': 12787, 'author_profile': 'https://Stackoverflow.com/users/12787', 'pm_score': 1, 'selected': False, 'text': '<p>If you need to access multiple users, then <a href="https://stackoverflow.com/questions/61995/xslt-and-xpath-match-conditionally-upon-current-node-value#62429">JeniT\'s <code>&lt;xsl:key /&gt;</code> approach</a> is ideal.</p>\n\n<p>Here is my alternative take on it:</p>\n\n<pre><code>&lt;xsl:template match="current/login_name"&gt;\n    &lt;xsl:variable name="person" select="//people/person[login_name = .]" /&gt;\n    &lt;xsl:value-of select="concat($person/first, \' \', $person/last)" /&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n\n<p>We assign the selected <code>&lt;person&gt;</code> node to a variable, then we use the <code>concat()</code> function to output the first/last names.</p>\n\n<p>There is also an error in your example XML. The <code>&lt;person&gt;</code> node incorrectly ends with <code>&lt;/preson&gt;</code> (typo)</p>\n\n<p>A better solution could be given if we knew the  overall structure of the XML document (with root nodes, etc.)</p>\n'}]
['https://Stackoverflow.com/questions/61995', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1532/']