如何使用xsl 1.0查找最小值和最大值?

use*_*464 3 xslt xslt-1.0

file_1.xml

<productlist>
    <items>
        <item>Pen</item>
        <price>8</item>
    </items>
    <items>
        <item>Pen</item>
        <price>5</item>
    </items>
    <items>
        <item>Pen</item>
        <price>10</item>
    </items>
    <items>
        <item>Bag</item>
        <price>15</item>
    </items>
    <items>
        <item>Bag</item>
        <price>22</item>
    </items>
    <items>
        <item>Bag</item>
        <price>20</item>
    </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)

file_2.xml

<productlist>
    <items>
        <item>Pen</item>
    </items>
    <items>
        <item>Bag</item>
    </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)

使用xsl 1.0需要输出如下最大值和最小值

<productlist>
    <items>
        <item>Pen</item>
        <min>5</min>
        <max>10</max>
    </items>
    <items>
        <item>Bag</item>
        <min>15</min>
        <max>22</max>
    </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)

Mar*_*nen 7

排序然后取第一个作为最小值,最后一个作为最大值:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="data-url" select="'file_1.xml'"/>
<xsl:variable name="data-doc" select="document($data-url)"/>

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="k1" match="items" use="item"/>

<xsl:template match="@* |  node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="items">
  <xsl:variable name="this" select="."/>
  <xsl:copy>
    <xsl:copy-of select="item"/>
    <xsl:for-each select="$data-doc">
      <xsl:for-each select="key('k1', $this/item)">
         <xsl:sort select="price" data-type="number" order="ascending"/>
         <xsl:if test="position() = 1">
           <min>
             <xsl:value-of select="price"/>
           </min>
         </xsl:if>
         <xsl:if test="position() = last()">
           <max>
             <xsl:value-of select="price"/>
           </max>
         </xsl:if>
       </xsl:for-each>
     </xsl:for-each>
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当我在输入文档上使用Saxon 6.5.5的上述样式表时

<productlist>
    <items>
        <item>Pen</item>
    </items>
    <items>
        <item>Bag</item>
    </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)

其他文件在哪里

<productlist>
    <items>
        <item>Pen</item>
        <price>8</price>
    </items>
    <items>
        <item>Pen</item>
        <price>5</price>
    </items>
    <items>
        <item>Pen</item>
        <price>10</price>
    </items>
    <items>
        <item>Bag</item>
        <price>15</price>
    </items>
    <items>
        <item>Bag</item>
        <price>22</price>
    </items>
    <items>
        <item>Bag</item>
        <price>20</price>
    </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)

我得到了想要的结果

<productlist>
   <items>
      <item>Pen</item>
      <min>5</min>
      <max>10</max>
   </items>
   <items>
      <item>Bag</item>
      <min>15</min>
      <max>22</max>
   </items>
</productlist>
Run Code Online (Sandbox Code Playgroud)