以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  [求助]如何选择一个最小值并显示?(再来一个更麻烦的问题)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=47389)


--  作者:Starling
--  发布时间:5/23/2007 11:54:00 AM

--  [求助]如何选择一个最小值并显示?(再来一个更麻烦的问题)
我现在正在制作一个报价表,XML文件返回的价格条目非常之多,但我只想显示同类商品中价格最低的一个,并且返回价格为 0 或为空的条目不显示,该怎么做呢??
XML代码示例如下:
<root>
<item>
  <name>item01</name>
  <price>1.5</price>
  <price>1.3</price>
  <price>1.0</price>
  <price>0.9</price>
  <price>0.75</price>
  <price>0</price>
</item>
<item>
  <name>item02</name>
  <price>1.5</price>
  <price>1.3</price>
  <price>1.0</price>
  <price>0.9</price>
  <price>0.8</price>
  <price>0.75</price>
  <price>0.70</price>
  <price>0.65</price>
  <price>0.60</price>
</item>
</root>

我用 when...otherwise 试了好久都不成功,在这里求教各位大牛了。


[此贴子已经被作者于2007-5-25 12:19:53编辑过]

--  作者:Starling
--  发布时间:5/24/2007 11:27:00 AM

--  
最后希望的显示效果如下:
<root>
<item>
  <name>item01</name>
  <price>0.75</price>
</item>
<item>
  <name>item02</name>
  <price>0.60</price>
</item>
</root>
也就是每个商品种类只显示最低价格,其它价格都不显示,而空格和0都忽略。


[此贴子已经被作者于2007-5-24 15:23:41编辑过]

--  作者:Starling
--  发布时间:5/24/2007 3:54:00 PM

--  
我用 XSL 的 <xsl:choose> 试验了很多次,但一直是以失败告终,真不知道该怎么弄了。
--  作者:Zjusan
--  发布时间:5/24/2007 4:06:00 PM

--  
可以用个递归去取最小值!
--  作者:Starling
--  发布时间:5/24/2007 4:10:00 PM

--  
老大可以给个例子点拨一下么?小弟先拜谢乐~~~

我是泥腿子,对各种术语都不甚了解,只能依葫芦画瓢。


--  作者:Zjusan
--  发布时间:5/24/2007 5:08:00 PM

--  
看看这样行不:
<xsl:template name="minprice">
  <xsl:param name="min"/>
  <xsl:param name="prices"/>  
  <xsl:choose>
    <xsl:when test="not($prices)">
      <xsl:value-of select="number($min)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="subprice" select="$prices[1]"/>
      <xsl:call-template name="minprice">
        <xsl:with-param name="prices" select="$prices[position( ) > 1]"/>
        <xsl:with-param name="max">
          <xsl:choose>
            <xsl:when test="number($prices) &lt; number($min)">
              <xsl:value-of select="$prices"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$min"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
--  作者:Starling
--  发布时间:5/24/2007 5:24:00 PM

--  
好像不行……………

无法解析到变量或参数 'min'的引用。变量或参数可能没有被定义,或它可能不在范围内。


--  作者:Zjusan
--  发布时间:5/24/2007 5:31:00 PM

--  
<xsl:with-param name="max">改成min就好了!

--  作者:Starling
--  发布时间:5/24/2007 5:37:00 PM

--  
只显示了“NaN”三个字母~~~
--  作者:gogy
--  发布时间:5/24/2007 5:44:00 PM

--  
其实很简单的一个问题。现在下班了,我回家跟你说。
--  作者:Qr
--  发布时间:5/24/2007 5:54:00 PM

--  
本版有一贴子是用“排序法”求最大值的,可用在此处取最小值,然后用<xsl:choose>判断空格和0即可。

“排序法”比上面的递归容易理解多了。自己搜一下旧贴,偶不帮忙搜了。


--  作者:Starling
--  发布时间:5/24/2007 6:01:00 PM

--  
谢啦,我去找找,呵呵。

多谢各位关心我这个小白乐~~~


--  作者:Starling
--  发布时间:5/24/2007 6:07:00 PM

--  
FT,搜不到帖子,难道删了??
--  作者:gogy
--  发布时间:5/24/2007 7:37:00 PM

--  
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="root">
   <xsl:copy>
     <xsl:apply-templates select="*"/>
   </xsl:copy>
 </xsl:template>
 <xsl:template match="item">
   <xsl:copy>
     <xsl:copy-of select="name"/>
     <xsl:for-each select="price[. &gt; 0 ]">
  <xsl:sort data-type="number"/>
  <xsl:if test="position()=1">
    <xsl:copy-of select="."/>
  </xsl:if>
  </xsl:for-each>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>
--  作者:Starling
--  发布时间:5/25/2007 8:03:00 AM

--  
挖靠~~这么简单的代码?!!楼上你真厉害~!!!
拜谢了,我再好好研究研究~~~~~

顺便再请教一个更有难度的问题。

如果XML文件是这样:

<root>

<type>
  <item>
   <name>001</name>
   <price>1.5</price>
  </item>
  <item>
   <name>002</name>
   <price>1.0</price>
  </item>
  <item>
   <name>002</name>
   <price>0.5</price>
  </item>
</type>

<type>
  <item>
   <name>004</name>
   <price>1.4</price>
  </item>
  <item>
   <name>005</name>
   <price>0.45</price>
  </item>
  <item>
   <name>006</name>
   <price>0.34</price>
  </item>
  <item>
   <name>007</name>
   <price>0</price>
  </item>
</type>

</root>

但这次是要显示每个 type 里标注了最低价的 item,同时显示它的 name 和 price,price 为 0 的当然还是不显示………

最后的效果应该是:
<root>

<type>
  <item>
   <name>002</name>
   <price>0.5</price>
  </item>
</type>

<type>
  <item>
   <name>006</name>
   <price>0.34</price>
  </item>
  <item>
</type>

</root>

这么做似乎增加了不少难度,各位高手带着小弟一起研究研究???

[此贴子已经被作者于2007-5-25 8:34:13编辑过]

--  作者:Starling
--  发布时间:5/25/2007 3:58:00 PM

--  
晕,自己解决了………

<xsl:copy-of select="."/>
后面加上
<xsl:copy-of select="../name"/>
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
93.750ms