title: XSLT: select all nodes that start with
body: <p>I am trying to do a XSLT transformation where the input XML is arbitrary. The only thing that's constant in it is that it'll have a node the name of which starts with 'first'. I need to get the value of that node and it's immediate sibling. However, the following template produces only the XML declaration.</p>
  
  <p>If it matters, this code is in Ruby using Nokogiri XML parser. However, I think this is more of a XSLT/XPath question rather than a Ruby question and hence tagging accordingly.</p>
  
  <p>Input XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;employees&gt;
    &lt;employee&gt;
      &lt;first_name&gt;Winnie&lt;/first_name&gt;
      &lt;last_name&gt;the Pooh&lt;/last_name&gt;
    &lt;/employee&gt;
    &lt;employee&gt;
      &lt;first_name&gt;Jamie&lt;/first_name&gt;
      &lt;last_name&gt;the Weeh&lt;/last_name&gt;
    &lt;/employee&gt;
  &lt;/employees&gt;
  </code></pre>
  
  <p>Desired output XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;people&gt;
    &lt;person&gt;
      &lt;first&gt;Winnie&lt;/first&gt;
      &lt;last&gt;the Pooh&lt;/last&gt;
    &lt;/person&gt;
    &lt;person&gt;
      &lt;first&gt;Jamie&lt;/first&gt;
      &lt;last&gt;the Weeh&lt;/last&gt;
    &lt;/person&gt;
  &lt;/people&gt;
  </code></pre>
  
  <p>XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" indent="yes" encoding="UTF-8" /&gt;
  &lt;xsl:template match="/"&gt;
    &lt;xsl:for-each select="node()[starts-with(name(), 'first')]"&gt;
      &lt;people&gt;
        &lt;person&gt;
          &lt;name&gt;
        &lt;first&gt;&lt;xsl:value-of select="." /&gt;&lt;/first&gt;
        &lt;last&gt;&lt;xsl:value-of select="following-sibling::*[1]" /&gt;&lt;/last&gt;
      &lt;/name&gt;
        &lt;/person&gt;
      &lt;/people&gt;
    &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;
  </code></pre>