的XPath与属性值的正则表达式匹配 [英] XPath with regex match on an attribute value

查看:2165
本文介绍了的XPath与属性值的正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部 -

我已经搜查,并在努力破解这个周围修修补补了几个小时,但我仍然有问题。我有下面的XML数据:

 <游戏ID =2009/05/02 / arimlb-milmlb-1PK =244539>
  <团队ID =109NAME =亚利桑那HOME_TEAM =false的>
    <事件数=9局=1说明=菲利普·洛佩兹加倍到左外野手克里斯·达菲。/>
    <事件编号=15局=1说明=奥吉·奥赫达飞出到外野手麦克卡梅伦/>
    <事件数=23局=1说明=乍得特雷西加倍到右外野手乔·桑切斯。/>
    <事件数=52局=2描述=马克·雷诺兹线出左外野手克里斯·达菲。/>
    <! - 在这里更多的数据 - >
  < /团队>
< /游戏>

我试图让包含在描述属性值的文本双打事件节点的总数。这是我一直很努力,到目前为止,无果(IRB引发错误):

 /游戏/团队/事件/ @描述[匹配(。,'双打')]

因为我只是想匹配的描述属性值的一个片段,它可以使用的XPath 2.0功能'匹配',对吧?如果是这样,我是什么做错了吗?

在此先感谢您的帮助!


解决方案

  

我试图让总数
  包含文本事件节点
  在值了双打
  描述属性。


匹配() 是一个标准的XPath 2.0功能。它不提供的XPath 1.0

您可以使用

 计数(/ * / * /事件[包含(@description,'双打')])

为了验证这一点,这里是刚刚输出所提供的XML文档评估上述的XPath前pression结果小XSLT转换:

 <的xsl:样式版本=1.0
 的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform>
 < XSL:输出方法=TEXT/>
 <的xsl:模板匹配=/>
  <的xsl:选择价值-=
  计数(/ * / * /事件[包含(@description,'双打')])/>
 < / XSL:模板>
< / XSL:样式>

时,这种转变被应用所提供的XML文档:

 <游戏ID =2009/05/02 / arimlb-milmlb-1PK =244539>
    <团队ID =109NAME =亚利桑那HOME_TEAM =false的>
        <事件数=9局=1说明=菲利普·洛佩兹加倍到左外野手克里斯·达菲。/>
        <事件编号=15局=1说明=奥吉·奥赫达飞出到外野手麦克卡梅伦/>
        <事件数=23局=1说明=乍得特雷西加倍到右外野手乔·桑切斯。/>
        <事件数=52局=2描述=马克·雷诺兹线出左外野手克里斯·达菲。/>
        <! - 在这里更多的数据 - >
    < /团队>
< /游戏>

通缉,正确的结果产生

  2

All -

I've searched and tinkered around for hours in an effort to crack this one, but I'm still having problems. I have the XML data below:

<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>

I'm trying to get the total number of event nodes that contain the text ' doubles ' in the value of the description attribute. This is what I've been trying so far, to no avail (irb throws an error):

"/game/team/event/@description[matches(.,' doubles ')]"

Since I'm just trying to match a fragment of the value of the description attribute, it's possible to use the XPath 2.0 function 'matches', right? If so, what am I doing wrong?

Thanks in advance for any help!

解决方案

I'm trying to get the total number of event nodes that contain the text ' doubles ' in the value of the description attribute.

matches() is a standard XPath 2.0 function. It is not available in XPath 1.0.

You can use:

count(/*/*/event[contains(@description, ' doubles ')])

To verify this, here is a small XSLT transformation which just outputs the result of evaluating the above XPath expression on the provided XML document:

<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>

when this transformation is applied on the provided XML document:

<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>

the wanted, correct result is produced:

2

这篇关于的XPath与属性值的正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆