所有 –
我搜索和修改了几个小时,努力破解这一个,但我仍然有问题。我有以下XML数据:
<game id="2009/05/02/arimlb-milmlb-1" pk="244539"> <team id="109" name="Arizona" home_team="false"> <event number="9" inning="1" description="Felipe Lopez doubles to left fielder Chris Duffy. "/> <event number="15" inning="1" description="Augie Ojeda flies out to center fielder Mike Cameron. "/> <event number="23" inning="1" description="Chad Tracy doubles to right fielder Joe Sanchez. "/> <event number="52" inning="2" description="Mark Reynolds lines out to left fielder Chris Duffy. "/> <!-- more data here --> </team> </game>
我试图获取的描述属性的值中包含文本’doubles’的事件节点的总数。这是我一直在尝试到目前为止,没有效果(irb抛出一个错误):
"/game/team/event/@description[matches(.,' doubles ')]"
因为我只是想匹配描述属性的值的一个片段,可以使用XPath 2.0函数’matches’,对不对?如果是这样,我做错了什么?
先感谢任何帮助!
I’m trying to get the total number of
event nodes that contain the text ‘
doubles ‘ in the value of the
description attribute.
matches()
是一个标准的XPath 2.0函数。它在XPath 1.0中不可用。
您可以使用:
count(/*/*/event[contains(@description,' doubles ')])
为了验证这一点,这里是一个小的XSLT转换,只是输出在提供的XML文档上评估上述XPath表达式的结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:value-of select= "count(/*/*/event[contains(@description,' doubles ')])"/> </xsl:template> </xsl:stylesheet>
当此转换应用于所提供的XML文档时:
<game id="2009/05/02/arimlb-milmlb-1" pk="244539"> <team id="109" name="Arizona" home_team="false"> <event number="9" inning="1" description="Felipe Lopez doubles to left fielder Chris Duffy. "/> <event number="15" inning="1" description="Augie Ojeda flies out to center fielder Mike Cameron. "/> <event number="23" inning="1" description="Chad Tracy doubles to right fielder Joe Sanchez. "/> <event number="52" inning="2" description="Mark Reynolds lines out to left fielder Chris Duffy. "/> <!-- more data here --> </team> </game>
产生想要的正确结果:
2