简体   繁体   English

访问JavaScript XPath查询中的节点集

[英]Accessing a node-set in a JavaScript XPath query

I have a real simple question that I can't seem to find an answer to. 我有一个简单的问题,我似乎无法找到答案。

I want to compress two XPath statements (that are getting attribute values). 我想压缩两个XPath语句(获取属性值)。 I learned about the | 我了解了| operator, hearing how it returns node sets. 运算符,听听它如何返回节点集。

var getdata = xmldoc.evaluate
(
    '/foo/bar[@world=\''+hello+'\']/child::*/attribute::name
    |/foo/bar[@world=\''hello+'\']/child::*/attribute::id', 
    xmldoc, null, XPathResult.ANY_TYPE, null
);

To anyone wondering, no I do not format my evaluation strings that way ... though, I sort of like it now that I typed it out. 对于任何想知道的人,不,我不会以这种方式格式化我的评估字符串...尽管如此,我现在有点像我输入它了。 Anyways, this is how I tested it out. 无论如何,这就是我测试它的方式。

alert(getItemData.iterateNext().childNodes[0].nodeValue);

That works! 这样可行! But it only returns the first one. 但它只返回第一个。 While writing this, I just tried .length and made a break through ... it's only counting one item. 在写这篇文章时,我只是尝试了.length并突破了...它只计算一个项目。 Was I deceived about the concept of | 我是否被欺骗了|的概念 ? How can I get a set and then go through them? 我如何获得一套然后通过它们?

XML document, as requested. XML文档,根据要求。

<?xml version="1.0" encoding="ISO-8859-1"?>
<foo>
    <bar world="hello" id="1">
        <subbar name="item1" id="2">
        </subbar>
    </bar>
    <bar world="bye" id="3">
        <subbar name="item2" id="4">
        </subbar>
    </bar>
</foo>

Edit: I am currently using a function that grabs the element rather than the attribute, but I would really like to know the other way. 编辑:我目前正在使用一个抓取元素而不是属性的函数,但我真的想知道另一种方式。 Unless what I am doing is the best way. 除非我正在做的是最好的方式。

If JQuery is an option, it might be worth your while to check out their XML traversal library. 如果JQuery是一个选项,那么查看他们的XML遍历库可能是值得的。 A quick search pulled up an article here . 快速搜索在这里拉了一篇文章。 I wrote up a very rough example of what the logic may look like after you import the xml document, which is explained in the link. 我写了一个非常粗略的例子,说明导入xml文档后逻辑的样子,链接中对此进行了解释。

var hello = "foo";
$('bar[world=' + hello + '] > subbar').each(function () {
    // You'd want to save these values somewhere else, obviously.
    $(this).getAttribute(name);
    $(this).getAttribute(id);
});

The key here is the XPathResult type you use. 这里的关键是你使用的XPathResult类型。

I have implemented a working sample for the same. 我已经实现了同样的工作样本。 Please refer the code at http://jsbin.com/eneso3/5/edit 请参阅http://jsbin.com/eneso3/5/edit上的代码

Basically you have to use Iterator as result type sot hat we can iterate through them to get the text. 基本上你必须使用Iterator作为结果类型sot hat我们可以迭代它们来获取文本。 Refer Xpath reference mentioned on the working code sample page. 请参阅工作代码示例页面上提到的Xpath参考。

Well your usage of the "pipe" is correct ( http://www.tizag.com/xmlTutorial/xpathbar.php ) so the only code that I can see might be off is a missing + in the second xpath command, but that might be pseudo code, so I would only count this as a half answer. 那么你对“管道”的使用是正确的( http://www.tizag.com/xmlTutorial/xpathbar.php )所以我能看到的唯一代码可能是第二个xpath命令中缺少的+,但是可能是伪代码,所以我只把它算作半答案。 As for the best practice, in my opinion I would grab the subbar element then grab it's attributes out where you need them an optimization like the one you've suggested obfuscates what data is being referenced. 至于最佳实践,在我看来,我会抓住子栏元素,然后抓住它的属性,在那里你需要一个优化,就像你建议的那样,模糊了被引用的数据。 Seems too much of a mico-optimization, but this is just an opinion. 似乎太多的微型优化,但这只是一个意见。 Maybe you have a long list of attributes and you really are saving parsing time. 也许你有很长的属性列表,你真的在​​节省解析时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM