使用xpath导航到XML部分 [英] navigate to section of XML with xpath

查看:97
本文介绍了使用xpath导航到XML部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法看到我的xpath逻辑出了问题.

i am not able to see where i am going wrong with my xpath logic.

这是我正在处理的较大xml的一部分. (请注意我使用Html Agility Pack)

here is a section of a larger xml that i am working on transversing. (note im using the Html Agility Pack)

<div> 
    <div></div>
    <span class="pp-headline-item pp-headline-phone"> 
        <span class="telephone" dir="ltr"> 
            <nobr>(732) 562-1312</nobr> 
            <span class="pp-headline-phone-label" style="display:none">()</span>
        </span>&#8206;
    </span>  
    <span> &middot; </span> 
    <span class="pp-headline-item pp-headline-authority-page"> 
        <span>
            <a href="http://maps.google.com/local_url?q=http://www.fed.com/q=07746+pizza">
                <span>fed.com</span>
            </a>
        </span> 
    </span>  
</div>

我的目标是使用

.SelectNodes("//div/span['pp-headline-item pp-headline-phone']/../..")

与此相关的是,我希望能够获得上面概述的所有部分,以便我可以对其进行迭代并提取网站,电话,地址...

with this i am expecting to get all the sections outlined above so i can iterate them and extract things like website, phone, address...

问题是当我迭代此节点集时,我无法到达想要的数据点,就好像该节点集不是顶部概述的那样.

problem is when i iterate this nodeset i cant get to the data points i want as if the node set is not the one outlined on top.

我的逻辑是从最上层的div中提取一个节点集到nodset中,然后在将它们迭代到xpath到我想要的数据点中时.

my logic is to extract a nodeset from the top most div into the nodset and when iterating them to xpath into the data points i want.

我这样做:

foreach (HtmlNode n in BuizRowsgoogMaps)
                {                    
                    //get phone number
                    if (n.SelectSingleNode("span/nobr").InnerHtml != null)
                    {
                        strPhone = n.SelectSingleNode("span/nobr").InnerHtml;

                        //get phone site
                        strSite = n.SelectSingleNode("//span['pp-headline-item pp-headline-authority-page']/span/a/span").InnerHtml;
                     }
                }

我怀疑我的xpaths不能啮合在一起以获得我想要的东西,但是当我验证我的表达式时,我得到了想要的结果...我用它来验证我的想法,它的工作使我无所适从:

i suspect my xpaths dont mesh together to get what i want but when i validate my expression i get the desired results... i used this to validate my thinking and it works leaving me at wits end:

//div/span['pp-headline-item pp-headline-phone']/../../span['pp-headline-item pp-headline-phone']/span/nobr

推荐答案

您的代码几乎正确,您只需要稍微修改一下xpath.

Your code is almost right, you just need to modify your xpath a bit.

foreach (HtmlNode n in BuizRowsgoogMaps)
{
  //get phone number
  if (n.SelectSingleNode(".//span/nobr").InnerHtml != null)
  {
    strPhone = n.SelectSingleNode(".//span/nobr").InnerHtml;

    //get phone site
    strSite = n.SelectSingleNode(".//span['pp-headline-item pp-headline-authority-page']/span/a/span").InnerHtml;
  }
}

.//告诉xpath从当前节点而不是根节点进行匹配.

The .// tells xpath to match from the current node and not from the root.

这篇关于使用xpath导航到XML部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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