xpath 2.0 语句转 1.0,该怎么写?

budded 2009-12-11 04:09:06
xml文档范例:
<root>
<item name="Jack" a="1" b="2" c="3"/>
<item name="Rich" a="2" b="2" c="3"/>
<item name="Jack" a="3" c="3"/>
</root>
现在要统计name="Jack"的数据项,得到c-b+a的值并累加,
用xpath2.0很好办,sum(//item[@name="Jack"]/(@c -@b +@a)) 就可以搞定,
现在用msxml4.0解析器,只支持xpath1.0,怎样改上面的xpath语句啊?或者用别的方法?
先谢谢了

...全文
70 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2009-12-17
  • 打赏
  • 举报
回复

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="root/item">
<xsl:choose>
<xsl:when test = "@b">
<xsl:value-of select="number(@c)-number(@b)+number(@a)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="number(@c)+number(@a)" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
孟子E章 2009-12-17
  • 打赏
  • 举报
回复
number(//item[@name='Jack']/@c)-number(//item[@name='Jack']/@b)+number(//item[@name='Jack']/@a)
budded 2009-12-17
  • 打赏
  • 举报
回复
to #3, 俺不懂网页,

继续顶
budded 2009-12-17
  • 打赏
  • 举报
回复
谢谢各位!
skyboxgogo 2009-12-17
  • 打赏
  • 举报
回复
楼上的正解
budded 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yao_ming_01 的回复:]
sum(//item[@name="Jack"]/(@c -@b +@a)) xpath2.0的这句不对啊
[/Quote]
这个用xmlspy验证过,没问题哦
kaleon 2009-12-14
  • 打赏
  • 举报
回复

<root>
<item name="Jack" a="1" b="2" c="3"/>
<item name="Rich" a="2" b="2" c="3"/>
<item name="Jack" a="3" b="2" c="3"/>
</root>



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ckbk="ckbk" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="//item[@name='Jack']"/>
<xsl:with-param name="result" select="0"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:sum">
<!-- Initialize nodes to empty node set -->
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<!-- call or apply template that will determine value of node
unless the node is literally the value to be summed -->
<xsl:variable name="value" select="$nodes[1]/@c - $nodes[1]/@b + $nodes[1]/@a"/>
<!-- recurse to sum rest -->
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
<xsl:with-param name="result" select="$result + $value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

yao_ming_01 2009-12-11
  • 打赏
  • 举报
回复
sum(//item[@name="Jack"]/(@c -@b +@a)) xpath2.0的这句不对啊
budded 2009-12-11
  • 打赏
  • 举报
回复
自己顶

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧