2012-10-11 106 views
3

file_1.xml如何使用xsl 1.0找到最小值和最大值?

<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> 

file_2.xml

<productlist> 
    <items> 
     <item>Pen</item> 
    </items> 
    <items> 
     <item>Bag</item> 
    </items> 
</productlist> 

需要出放像下面的最大和最小值使用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> 

回答

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> 

當我申請上述樣式與撒克遜6.5.5對輸入文檔

<productlist> 
    <items> 
     <item>Pen</item> 
    </items> 
    <items> 
     <item>Bag</item> 
    </items> 
</productlist> 

在另一個文件是

<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> 

我得到了想要的結果

<productlist> 
    <items> 
     <item>Pen</item> 
     <min>5</min> 
     <max>10</max> 
    </items> 
    <items> 
     <item>Bag</item> 
     <min>15</min> 
     <max>22</max> 
    </items> 
</productlist> 
-1
<xsl:stylesheet version="2.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="/"> 
     <xsl:apply-templates select="productlist"/> 
    </xsl:template> 
    <xsl:template match="productlist"> 
     <productlist> 
      <xsl:variable name="intermediate"> 
       <xsl:for-each-group select="items" group-by="item"> 
        <items> 
         <item> 
          <xsl:value-of select="item"/> 
         </item> 
         <prices> 
          <xsl:for-each select="current-group()"> 
           <price> 
            <xsl:value-of select="price"/> 
           </price> 
          </xsl:for-each> 
         </prices> 
        </items> 
       </xsl:for-each-group> 
      </xsl:variable> 
      <xsl:for-each select="$intermediate/items"> 
       <items> 
        <item> 
         <xsl:value-of select="item"/> 
        </item> 
        <xsl:for-each select="prices"> 
         <min> 
          <xsl:value-of select="min(price)"/> 
         </min> 
         <max> 
          <xsl:value-of select="max(price)"/> 
         </max> 
        </xsl:for-each> 
       </items> 
      </xsl:for-each> 
     </productlist> 
    </xsl:template> 
</xsl:stylesheet> 
+3

尼斯。如果只有OP沒有明確要求XSLT 1.0! –