+ -
当前位置:首页 → 问答吧 → 用XSL筛选XML数据遇到的问题

用XSL筛选XML数据遇到的问题

时间:2007-05-24

来源:互联网

我现在正在制作一个报价表,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>

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

我用 XSL 的 <xsl:choose> 试验了很多次,但一直是以失败告终,真不知道该怎么弄了。

有人提示我用递归取最小值,但无奈我是初学者,实在不知道该怎么整。

在这里求各位大牛给点拨一下了,小弟感激不尽~~~

作者: Starling   发布时间: 2007-05-24

问题解决了,也是请教高手得到的答案……比我自己想的方法简单多了………

<?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   发布时间: 2007-05-25

没有用过<xsl:copy>,但还是要顶!

作者: solidluck   发布时间: 2007-08-18

后来证明其实不用 copy 也可以的,它只是简单的复制罢了。

作者: Starling   发布时间: 2007-08-22