title: XSLT sort by tag name and attribute value
body: <p>I'm a noob with XSLT, so please excuse my ignorance...
  I'm trying to sort a simple XML file by attribute value and tag name, but I struggle in accessing the value of the attribute.
  Here is a complete example:</p>
  
  <pre><code>&lt;a&gt;
      &lt;b attribute="e"&gt;&lt;/b&gt;
      &lt;b attribute="b"&gt;&lt;/b&gt;
      &lt;d attribute="a"&gt;&lt;/d&gt;
      &lt;c&gt;&lt;/c&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>And the expected result is:</p>
  
  <pre><code>&lt;a&gt;
      &lt;b attribute="b"&gt;&lt;/b&gt;
      &lt;b attribute="e"&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
      &lt;d attribute="a"&gt;&lt;/d&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Here is my attempt to solve this:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output indent="yes" /&gt;
      &lt;xsl:strip-space elements="*" /&gt;
  
      &lt;xsl:template match="node()|@*"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*"&gt;
                  &lt;xsl:sort select="."/&gt;
              &lt;/xsl:apply-templates&gt;
  
              &lt;xsl:apply-templates select="node()"&gt;
                  &lt;xsl:sort select="name()"/&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And this obviously don't work at all...</p>
  
  <p>In the above example I want to sort the <em>b</em> tag by their attribute value but as you can see the <em>d</em> tag is not sorted by attribute value because it's another tag name...</p>
  
  <p>I wonder if this is possible using XSLT...
  Do you have an idea?</p>
  
  <p>Thanks in advance.  </p>
  
  <p><strong>UPDATE----------------------</strong></p>
  
  <p>I tried andyb solution that seems to work fine and looks pretty simple, but I have another issue with this solution.</p>
  
  <p>Let's say I have this XML:</p>
  
  <pre><code>&lt;a&gt;
      &lt;b attribute="e" optionalAttr="fg"&gt;&lt;/b&gt;
      &lt;b attribute="b"&gt;&lt;/b&gt;
      &lt;d attribute="a"&gt;&lt;/d&gt;
      &lt;c&gt;&lt;/c&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I added an optional parameter for the <em>b</em> tag.
  Applying andyb solution the optional parameter will be ignored, because it is not matched in the template. Here is the result:</p>
  
  <pre><code>&lt;a&gt;
      &lt;b attribute="b"&gt;&lt;/b&gt;
      &lt;b attribute="e"&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
      &lt;d attribute="a"&gt;&lt;/d&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Instead of the following which is what I expect:</p>
  
  <pre><code>&lt;a&gt;
      &lt;b attribute="b"&gt;&lt;/b&gt;
      &lt;b attribute="e" optionalAttr="fg"&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
      &lt;d attribute="a"&gt;&lt;/d&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Do you have any idea?
  Thanks in advance.</p>