Match conditionally upon current node value


Given the following XML:


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

I tried the following:

<xsl:template match="current/login_name">
  <xsl:value-of select="../people/first[login_name = .]"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="../people/last[login_name = .]"/>

Best Solution

I'd define a key to index the people:

<xsl:key name="people" match="person" use="login_name" />

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 <person> elements based on their <login_name> child.

I'd have a template that returned the formatted name of a given <person>:

<xsl:template match="person" mode="name">
  <xsl:value-of select="concat(first, ' ', last)" />

And then I'd do:

<xsl:template match="current/login_name">
  <xsl:apply-templates select="key('people', .)" mode="name" />