XSLT - 选择最小、最大和平均属性值 - 需要来自不同级别的数据

XSLT - Selecting Min, Max, and Avg Attribute Values - Need Data from different Levels

我认为我的问题是该属性位于我需要 select 的节点下方的几个节点上。此外,我需要从不同层面提取价值。不确定如何构建 XPath。

<?xml version="1.0"?>
<software_inventory>
  <software xmlns:xsi="Software.xsd">
    <title>Adobe Photoshop</title>
    <vendor>Adobe</vendor>
    <category>Graphics</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
      <platform>Linux</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="399">CS 5.5</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Winzip</title>
    <vendor>Winzip International</vendor>
    <category>Utility</category>
    <support_platforms>
      <platform>Windows Vista</platform>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="2GB Ram 250MB Hard Drive" Software_Requiremnts="32/64 bit" Price="29.99">19</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Office 365</title>
    <vendor>Microsoft</vendor>
    <category>Productivity</category>
    <support_platforms>
      <platform>Windows Vista</platform>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="99">Office 365</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Visual Studio</title>
    <vendor>Microsoft</vendor>
    <category>Development</category>
    <support_platforms>      
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 2GB Hard Drive" Software_Requiremnts="32/64 bit" Price="1199">2013</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Google Chrome</title>
    <vendor>Google</vendor>
    <category>Productivity</category>
    <support_platforms>
      <platforms>Windows Vista</platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="1GB Ram 250MB Hard Drive" Software_Requiremnts="32/64 bit" Price="0">23</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Microsoft SQL Server 2012</title>
    <vendor>Microsoft</vendor>
    <category>DBMS</category>
    <support_platforms>      
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
      <platform>Windows Server 2012</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 4GB Hard Drive" Software_Requiremnts="64 bit" Price="3990">2012</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Microsoft Paint</title>
    <vendor>Microsoft</vendor>
    <category>Graphics</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
      <platform>Linux</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="16GB Ram 1TB Hard Drive" Software_Requiremnts="32/64 bit" Price="1000000">22</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Notepad</title>
    <vendor>Microsoft</vendor>
    <category>Development</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="32GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="500000">3</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Oracle Database</title>
    <vendor>Oracle</vendor>
    <category>DBMS</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
      <platform>Windows Server 2012</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="8GB Ram 4GB Hard Drive" Software_Requiremnts="64 bit" Price="4000">Enterprise Edition</Version>
      <Version Hardware_Requirements="8GB Ram 3GB Hard Drive" Software_Requiremnts="64 bit" Price="3000">Standard Edition</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Adobe Dreamweaver</title>
    <vendor>Adobe</vendor>
    <category>Development</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="2GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="299">CS5</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Adobe Fireworks</title>
    <vendor>Adobe</vendor>
    <category>Graphics</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="2GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="1">CS5</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Adobe Flash</title>
    <vendor>Adobe</vendor>
    <category>Development</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 1GB Hard Drive" Software_Requiremnts="32/64 bit" Price="499">CS5</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Adobe Illustrator</title>
    <vendor>Adobe</vendor>
    <category>Development</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="499">CS5</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Google Earth Pro</title>
    <vendor>Google</vendor>
    <category>Productivity</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="999">14</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Eclipse</title>
    <vendor>The Eclipse Foundation</vendor>
    <category>Development</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="4GB Ram 500MB Hard Drive" Software_Requiremnts="32/64 bit" Price="0">12</Version>
    </Approved_Versions>
  </software>
  <software>
    <title>Candy Crush Saga</title>
    <vendor>King.com ltd.</vendor>
    <category>Productivity</category>
    <support_platforms>
      <platform>Windows 7</platform>
      <platform>Windows 8</platform>
      <platform>Windows 8.1</platform>
    </support_platforms>
    <Approved_Versions>
      <Version Hardware_Requirements="32GB Ram 500GB Hard Drive" Software_Requiremnts="32/64 bit" Price="2000000">99</Version>
    </Approved_Versions>
  </software>
</software_inventory>

我需要 select 在 linux(平台)上运行的所有图形软件(类别),并且我需要最低、最高和平均价格(版本/@price)。这对我来说似乎很复杂,我真的需要一些帮助。

谢谢,

大卫

好的,这是对我有用的最终 XSLT。

<?xml version="1.0" encoding="utf-8"?>
<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">
    <xsl:output method="html"/>
    <xsl:template match="/software_inventory" >
      <html>
        <head>
          <title>Software</title>
        </head>
        <body>
          <h1>Software Titles for each category, sorted by price</h1>          
          <table border="1">
            <tr>
              <th>Software Title</th>
              <th>Category</th>
              <th>Price</th>              
            </tr>
            <xsl:for-each select="software">
              <xsl:sort select="category"/>
              <xsl:sort select="Approved_Versions/Version/@Price" data-type="number"/>              
              <tr>
                <td>
                  <xsl:value-of select="title"/>
                </td>
                <td>
                  <xsl:value-of select="category"/>
                </td>
                <td>$
                  <xsl:value-of select="Approved_Versions/Version/@Price"/>
                </td>                
              </tr>
            </xsl:for-each>
          </table>
          
          <h1>Software Titles for each operating system, sorted by category</h1>
          <table border="1">
            <tr>
              <th>Software Title</th>
              <th>Operating System</th>
              <th>Category</th>
            </tr>
            <xsl:for-each select="software">              
              <xsl:sort select="category"/>
              <tr>
                <td>
                  <xsl:value-of select="title"/>
                </td>                
                <td>  
                  <xsl:for-each select="support_platforms/platform">                    
                  <xsl:value-of select="."/>,
                    <xsl:text>&#xA;&#xD;</xsl:text>                    
                    </xsl:for-each>
                </td>                
                <td>                 
                  <xsl:value-of select="category"/>
                </td>
              </tr>
            </xsl:for-each>
          </table>

          <h1>Highest Priced Graphics Program for the Linux Ooperating System</h1>
          <xsl:variable name="selected-versions" select="software[category='Graphics' and support_platforms/platform='Linux']/Approved_Versions/Version" />
    <table border="1">
        <tr>
            <th>Minimum</th>
            <th>Maximum</th>
            <th>Average</th>              
        </tr>
        <tr>
            <xsl:for-each select="$selected-versions">
                <xsl:sort select="@Price" data-type="number" order="ascending"/>
                <xsl:if test="position()=1">
                    <td><xsl:value-of select="@Price"/></td>
                </xsl:if>
                <xsl:if test="position()=last()">
                    <td><xsl:value-of select="@Price"/></td>
                </xsl:if>
            </xsl:for-each>
            <td><xsl:value-of select="sum($selected-versions/@Price) div count($selected-versions)"/></td>
        </tr>
    </table>
        </body>
      </html>
        
    </xsl:template>
</xsl:stylesheet>

I need to select all graphics software (category) that runs on linux (platform) and I need the minimum, maximum, and average price (version/@price).

如果我怀疑您正在使用不支持 EXSLT 扩展功能的 XSLT 1.0,请尝试以下方法:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/software_inventory">
    <xsl:variable name="selected-versions" select="software[category='Graphics' and support_platforms/platform='Linux']/Approved_Versions/Version" />
    <table border="1">
        <tr>
            <th>Minimum</th>
            <th>Maximum</th>
            <th>Average</th>              
        </tr>
        <tr>
            <xsl:for-each select="$selected-versions">
                <xsl:sort select="@Price" data-type="number" order="ascending"/>
                <xsl:if test="position()=1">
                    <td><xsl:value-of select="@Price"/></td>
                </xsl:if>
                <xsl:if test="position()=last()">
                    <td><xsl:value-of select="@Price"/></td>
                </xsl:if>
            </xsl:for-each>
            <td><xsl:value-of select="sum($selected-versions/@Price) div count($selected-versions)"/></td>
        </tr>
    </table>
</xsl:template>

</xsl:stylesheet>

应用于您的示例,结果将是;

<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
   <tr>
      <th>Minimum</th>
      <th>Maximum</th>
      <th>Average</th>
   </tr>
   <tr>
      <td>399</td>
      <td>1000000</td>
      <td>500199.5</td>
   </tr>
</table>