Objective-C NodesForXPath 返回所有结果

发布于 2024-11-17 06:00:34 字数 1577 浏览 1 评论 0原文

这是我昨天发布的问题的后续。

鉴于下面发布的汽车 XML 示例,我运行 xpath 查询,循环遍历结果并使用子元素调用“quickquery - getString”。我希望在循环的每次迭代中我都会在 getString 函数中得到一个元素,但我没有。相反,getString 函数内部的nodesForXPath 调用会返回所有4 个汽车名称,而不仅仅是属于该子元素的汽车名称。

NSArray *listings = [response nodesForXPath:@"//car" error:&error];

//4 car elements found        
if(listings.count > 0)
{
    for (GDataXMLElement *listingElement in listings) 
    {
        //printing listingElements description at this point reveales only one car
        [QuickQuery getString:listingElement fromXPath:@"//name"];
    }
}

//this is defined in QuickQuery class
+(NSString *) getString:(GDataXMLElement *)element fromXPath:(NSString *)xPath
{
    NSError *error;
    //Query the name element for the spcific car element passed in
    NSArray *result = [element nodesForXPath:xPath error:&error]; /// ***THIS CALL

    //result.count is 4 at this stage ("BMW", "VW" ... etc)
    //out of the 4 calls made to this method, I would expect each value to come up once
    //but each time all 4 values are found.
    if(result.count > 0)
    {
        GDataXMLElement *singleValue = (GDataXMLElement *) [result objectAtIndex:0];
        return singleValue.stringValue;
    }
    return nil;
}


<cars>
  <car>
    <name>VW</name>
  </car>
  <car>
    <name>BMW</name>
  </car>
  <car>
    <name>Mazda</name>
  </car>
  <car>
    <name>Nissan</name>
  </car>
</cars>

这个问题之前已经发布过,这只是一个更清晰的示例和代码。标题也比较具体。

this is a follow up to a question I posted yesterday.

Given the cars XML example posted below, I run do an xpath query, loop through the results and call "quickquery - getString" with the sub element. I would expect that with each iteration of the loop I would get a single element inside the getString function, but I don't. Instead, the nodesForXPath call, inside of the getString function, returns all 4 car names, instead of just the one that belongs to that sub element.

NSArray *listings = [response nodesForXPath:@"//car" error:&error];

//4 car elements found        
if(listings.count > 0)
{
    for (GDataXMLElement *listingElement in listings) 
    {
        //printing listingElements description at this point reveales only one car
        [QuickQuery getString:listingElement fromXPath:@"//name"];
    }
}

//this is defined in QuickQuery class
+(NSString *) getString:(GDataXMLElement *)element fromXPath:(NSString *)xPath
{
    NSError *error;
    //Query the name element for the spcific car element passed in
    NSArray *result = [element nodesForXPath:xPath error:&error]; /// ***THIS CALL

    //result.count is 4 at this stage ("BMW", "VW" ... etc)
    //out of the 4 calls made to this method, I would expect each value to come up once
    //but each time all 4 values are found.
    if(result.count > 0)
    {
        GDataXMLElement *singleValue = (GDataXMLElement *) [result objectAtIndex:0];
        return singleValue.stringValue;
    }
    return nil;
}


<cars>
  <car>
    <name>VW</name>
  </car>
  <car>
    <name>BMW</name>
  </car>
  <car>
    <name>Mazda</name>
  </car>
  <car>
    <name>Nissan</name>
  </car>
</cars>

This question has been posted before, this is just a cleaner example and code. The title is also more specific.

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

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

发布评论

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

评论(1

青巷忧颜 2024-11-24 06:00:34

您所看到的是查询“//name”的正确行为。您应该使用相对于当前节点的查询,而不是文档的根 - 这就是您正在做的事情。

查看有用的 XPath 教程 http://www.w3schools.com/xpath/

What you are seeing is correct behaviour for the query "//name". You should use a query relative to the current node, not the root of the document - which is what you are doing.

Take a look at the useful XPath tutorial http://www.w3schools.com/xpath/

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