用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> 试验了很多次,但一直是以失败告终,真不知道该怎么弄了。
有人提示我用递归取最小值,但无奈我是初学者,实在不知道该怎么整。
在这里求各位大牛给点拨一下了,小弟感激不尽~~~
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[. > 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>
<?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[. > 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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28