title: XSLT - Selecting Min, Max, and Avg Attribute Values - Need Data from different Levels
body: <p>I thing my problem is that the attribute is on a couple of nodes down from the node that I need to select. Also, I need to pull values from different levels. Not sure how to construct the XPath.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;software_inventory&gt;
    &lt;software xmlns:xsi="Software.xsd"&gt;
      &lt;title&gt;Adobe Photoshop&lt;/title&gt;
      &lt;vendor&gt;Adobe&lt;/vendor&gt;
      &lt;category&gt;Graphics&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
        &lt;platform&gt;Linux&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="399"&gt;CS 5.5&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Winzip&lt;/title&gt;
      &lt;vendor&gt;Winzip International&lt;/vendor&gt;
      &lt;category&gt;Utility&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows Vista&lt;/platform&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="2GB Ram 250MB Hard Drive" Software_Requiremnts="32/64 bit" Price="29.99"&gt;19&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Office 365&lt;/title&gt;
      &lt;vendor&gt;Microsoft&lt;/vendor&gt;
      &lt;category&gt;Productivity&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows Vista&lt;/platform&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="99"&gt;Office 365&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Visual Studio&lt;/title&gt;
      &lt;vendor&gt;Microsoft&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;      
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="1199"&gt;2013&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Google Chrome&lt;/title&gt;
      &lt;vendor&gt;Google&lt;/vendor&gt;
      &lt;category&gt;Productivity&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platforms&gt;Windows Vista&lt;/platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="1GB Ram 250MB Hard Drive" Software_Requiremnts="32/64 bit" Price="0"&gt;23&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Microsoft SQL Server 2012&lt;/title&gt;
      &lt;vendor&gt;Microsoft&lt;/vendor&gt;
      &lt;category&gt;DBMS&lt;/category&gt;
      &lt;support_platforms&gt;      
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
        &lt;platform&gt;Windows Server 2012&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 4GB Hard Drive" Software_Requiremnts="64 bit" Price="3990"&gt;2012&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Microsoft Paint&lt;/title&gt;
      &lt;vendor&gt;Microsoft&lt;/vendor&gt;
      &lt;category&gt;Graphics&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
        &lt;platform&gt;Linux&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="16GB Ram 1TB Hard Drive" Software_Requiremnts="32/64 bit" Price="1000000"&gt;22&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Notepad&lt;/title&gt;
      &lt;vendor&gt;Microsoft&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="32GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="500000"&gt;3&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Oracle Database&lt;/title&gt;
      &lt;vendor&gt;Oracle&lt;/vendor&gt;
      &lt;category&gt;DBMS&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
        &lt;platform&gt;Windows Server 2012&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="8GB Ram 4GB Hard Drive" Software_Requiremnts="64 bit" Price="4000"&gt;Enterprise Edition&lt;/Version&gt;
        &lt;Version Hardware_Requirements="8GB Ram 3GB Hard Drive" Software_Requiremnts="64 bit" Price="3000"&gt;Standard Edition&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Adobe Dreamweaver&lt;/title&gt;
      &lt;vendor&gt;Adobe&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="2GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="299"&gt;CS5&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Adobe Fireworks&lt;/title&gt;
      &lt;vendor&gt;Adobe&lt;/vendor&gt;
      &lt;category&gt;Graphics&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="2GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="1"&gt;CS5&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Adobe Flash&lt;/title&gt;
      &lt;vendor&gt;Adobe&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 1GB Hard Drive" Software_Requiremnts="32/64 bit" Price="499"&gt;CS5&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Adobe Illustrator&lt;/title&gt;
      &lt;vendor&gt;Adobe&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="499"&gt;CS5&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Google Earth Pro&lt;/title&gt;
      &lt;vendor&gt;Google&lt;/vendor&gt;
      &lt;category&gt;Productivity&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="999"&gt;14&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Eclipse&lt;/title&gt;
      &lt;vendor&gt;The Eclipse Foundation&lt;/vendor&gt;
      &lt;category&gt;Development&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="0"&gt;12&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
    &lt;software&gt;
      &lt;title&gt;Candy Crush Saga&lt;/title&gt;
      &lt;vendor&gt;King.com ltd.&lt;/vendor&gt;
      &lt;category&gt;Productivity&lt;/category&gt;
      &lt;support_platforms&gt;
        &lt;platform&gt;Windows 7&lt;/platform&gt;
        &lt;platform&gt;Windows 8&lt;/platform&gt;
        &lt;platform&gt;Windows 8.1&lt;/platform&gt;
      &lt;/support_platforms&gt;
      &lt;Approved_Versions&gt;
        &lt;Version Hardware_Requirements="32GB Ram 500GB Hard Drive" Software_Requiremnts="32/64 bit" Price="2000000"&gt;99&lt;/Version&gt;
      &lt;/Approved_Versions&gt;
    &lt;/software&gt;
  &lt;/software_inventory&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I need to select all graphics software (category) that runs on linux (platform) and I need the minimum, maximum, and average price (version/@price). This seems very complex to me, and I really need some help.</p>
  
  <p>Thanks,</p>
  
  <p>David</p>
  
  <p>Ok, here is the final XSLT that worked for me. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"&gt;
      &lt;xsl:output method="html"/&gt;
      &lt;xsl:template match="/software_inventory" &gt;
        &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;Software&lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
            &lt;h1&gt;Software Titles for each category, sorted by price&lt;/h1&gt;          
            &lt;table border="1"&gt;
              &lt;tr&gt;
                &lt;th&gt;Software Title&lt;/th&gt;
                &lt;th&gt;Category&lt;/th&gt;
                &lt;th&gt;Price&lt;/th&gt;              
              &lt;/tr&gt;
              &lt;xsl:for-each select="software"&gt;
                &lt;xsl:sort select="category"/&gt;
                &lt;xsl:sort select="Approved_Versions/Version/@Price" data-type="number"/&gt;              
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;xsl:value-of select="title"/&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;xsl:value-of select="category"/&gt;
                  &lt;/td&gt;
                  &lt;td&gt;$
                    &lt;xsl:value-of select="Approved_Versions/Version/@Price"/&gt;
                  &lt;/td&gt;                
                &lt;/tr&gt;
              &lt;/xsl:for-each&gt;
            &lt;/table&gt;
            
            &lt;h1&gt;Software Titles for each operating system, sorted by category&lt;/h1&gt;
            &lt;table border="1"&gt;
              &lt;tr&gt;
                &lt;th&gt;Software Title&lt;/th&gt;
                &lt;th&gt;Operating System&lt;/th&gt;
                &lt;th&gt;Category&lt;/th&gt;
              &lt;/tr&gt;
              &lt;xsl:for-each select="software"&gt;              
                &lt;xsl:sort select="category"/&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;xsl:value-of select="title"/&gt;
                  &lt;/td&gt;                
                  &lt;td&gt;  
                    &lt;xsl:for-each select="support_platforms/platform"&gt;                    
                    &lt;xsl:value-of select="."/&gt;,
                      &lt;xsl:text&gt;&amp;#xA;&amp;#xD;&lt;/xsl:text&gt;                    
                      &lt;/xsl:for-each&gt;
                  &lt;/td&gt;                
                  &lt;td&gt;                 
                    &lt;xsl:value-of select="category"/&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/xsl:for-each&gt;
            &lt;/table&gt;
  
            &lt;h1&gt;Highest Priced Graphics Program for the Linux Ooperating System&lt;/h1&gt;
            &lt;xsl:variable name="selected-versions" select="software[category='Graphics' and support_platforms/platform='Linux']/Approved_Versions/Version" /&gt;
      &lt;table border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Minimum&lt;/th&gt;
              &lt;th&gt;Maximum&lt;/th&gt;
              &lt;th&gt;Average&lt;/th&gt;              
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;xsl:for-each select="$selected-versions"&gt;
                  &lt;xsl:sort select="@Price" data-type="number" order="ascending"/&gt;
                  &lt;xsl:if test="position()=1"&gt;
                      &lt;td&gt;&lt;xsl:value-of select="@Price"/&gt;&lt;/td&gt;
                  &lt;/xsl:if&gt;
                  &lt;xsl:if test="position()=last()"&gt;
                      &lt;td&gt;&lt;xsl:value-of select="@Price"/&gt;&lt;/td&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
              &lt;td&gt;&lt;xsl:value-of select="sum($selected-versions/@Price) div count($selected-versions)"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
          &lt;/body&gt;
        &lt;/html&gt;
          
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;</code></pre>
  </div>
  </div>
  </p>