XPath 祖先轴多个父项

发布于 2024-09-08 22:02:44 字数 2152 浏览 7 评论 0原文

所以我正在编写一个 Mac 应用程序,使用 xpath 解析 SF Giants 棒球统计数据。在我的情况下,我有一个人的特定数字标识符 1. (在我的应用程序中人们可以根据输入的数字搜索找到统计信息),但从那里我需要从 HTML 中的该位置退出,然后转到第一个节点,在这里我可以获取 HTML 底部的实际统计信息。

    <tr>  <! The node I need to back out into >

    <td><img src="/images/trans.gif" width="1" height="1" border="0" /></td>        
    <td>&nbsp;</td> 

    <td> 
        <table border="0" cellspacing="0" cellpadding="0"> 
            <tr> 
                <td class="textSm" align="right">1.&nbsp;</td> <! Number Identifier here >
                <td align="left" nowrap>
                      <a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a>
                </td> 
            </tr> 
        </table> 
    </td>   

    <td align="center">SF</td>  <! list of statistics starts here >
    <td align="center">3B</td> 
    <td align="center">83</td> 
    <td bgcolor="#CCCCCC" align="center">326</td> 
    <td align="center">41</td> 
    <td align="center">88</td> 
    <td align="center">18</td> 
    <td align="center">2</td> 
    <td align="center">6</td> 
    <td align="center">34</td> 
    <td align="center">128</td> 
    <td align="center">27</td> 
    <td align="center">45</td> 
    <td align="center">2</td> 
    <td align="center">2</td> 
    <td align="center">.324</td> 
    <td align="center">.393</td> 
    <td align="center">.270</td> 
</tr> 

到目前为止,我有这个 //table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr/td[starts-with(., '1.')] 这将找到我的号码标识符。我无法正常工作的是一直退出到统计信息所在的父节点 。我最好的猜测是执行 //table[@border=' 0' 和 @cellspacing='0' 和 @cellpadding='0']/tr/td[starts-with(., '1.')]/ancestor::tr/ancestor::table/ancestor::td/祖先::tr[1(或我想要的统计数据的任何其他数字]。任何感谢将不胜感激。

So I am writing a mac application that parses SF Giants baseball statistics using xpath. In my situation I have a specific number Identifier of a person <td class="textSm" align="right">1. </td>(in my application a person could search find a stat based on a number input) but from there I need to back out from that spot in the HTML, and go to the first node, where I can get to the actual statistics towards the bottom of the HTML.

    <tr>  <! The node I need to back out into >

    <td><img src="/images/trans.gif" width="1" height="1" border="0" /></td>        
    <td> </td> 

    <td> 
        <table border="0" cellspacing="0" cellpadding="0"> 
            <tr> 
                <td class="textSm" align="right">1. </td> <! Number Identifier here >
                <td align="left" nowrap>
                      <a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a>
                </td> 
            </tr> 
        </table> 
    </td>   

    <td align="center">SF</td>  <! list of statistics starts here >
    <td align="center">3B</td> 
    <td align="center">83</td> 
    <td bgcolor="#CCCCCC" align="center">326</td> 
    <td align="center">41</td> 
    <td align="center">88</td> 
    <td align="center">18</td> 
    <td align="center">2</td> 
    <td align="center">6</td> 
    <td align="center">34</td> 
    <td align="center">128</td> 
    <td align="center">27</td> 
    <td align="center">45</td> 
    <td align="center">2</td> 
    <td align="center">2</td> 
    <td align="center">.324</td> 
    <td align="center">.393</td> 
    <td align="center">.270</td> 
</tr> 

So far I have this //table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr/td[starts-with(., '1.')] which will find my number identifier. What I cant get working is backing out all the way to the parent node <tr> where the statistics are located in. My best guess is to do //table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr/td[starts-with(., '1.')]/ancestor::tr/ancestor::table/ancestor::td/ancestor::tr[1(or any other number for the statistic i want]. Any thanks would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-09-15 22:02:45

对于提供的 XML 片段,以下 XPath 表达式将定位似乎是所需节点的位置:

//table[@border='0' and @cellspacing='0' and @cellpadding='0']
          /tr/td[starts-with(., '1.')] 
                         /ancestor::tr[2]

这将选择第一个祖先 < 的第一个祖先 使用问题中提供的 XPath 表达式标识的节点的 /code> 。

For the XML fragment that is provided, the following XPath expression will locate what seems to be the desired node:

//table[@border='0' and @cellspacing='0' and @cellpadding='0']
          /tr/td[starts-with(., '1.')] 
                         /ancestor::tr[2]

This selects the first ancestor <tr> of the first ancestor <tr> of the node identified using the XPath expression provided in the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文