Search

I’m trying to select an element using a variable for part of the selector. Here’s an example:

What I’m trying to select:

<english-job-title handle="another-another-test-job">Another another test job!</english-job-title>
<cymraeg-job-title handle="arr">Arr</cymraeg-job-title>

I’m trying to use my $current-language variable to select the appropriate element, but I need a way of telling XSLT that the variable is finished. Here’s my current code:

<xsl:value-of select="$current-language-job-title"/>

Is there any way to put like a delimiter in there to tell it where the variable ends? eg:

<xsl:value-of select="$current-language&-job-title"/>

An XPath expression doesn’t support this natively — you can’t dynamically build a node name like that. However what you can do is find a node where the name equals a particular string:

<xsl:value-of select="*[name() = concat($current-language,'-job-title')]"/>

This will look for any direct child node (wildcard *) in the current scope where (using a predicate [...]) its node name (name() is an XPath function) equal to a string. The XSLT concat() function allows you to join strings together — you can have as many as you like inside the function, separated by commas.

Works like a charm. Thanks very much nick :)

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details