简体   繁体   English

JQuery的XPath选择器

[英]XPath selector for JQuery

I'm looking for a jQuery plugin or anything that will allow me to easily select elements through xpath after parsing an XML using $.parseXML . 我正在寻找一个jQuery插件或任何允许我在使用$.parseXML解析XML后通过xpath轻松选择元素的$.parseXML
There's no way to use CSS selectors, as that's a javascript port for a .NET program that already uses XPath selectors. 没有办法使用CSS选择器,因为这是一个已经使用XPath选择器的.NET程序的javascript端口。

I've seen a lot of questions asked on the matter, but couldn't see any viable answers, while it looks quite a basic need, and it came as a surprise when I learned it's not supported. 我已经看到了很多关于这个问题的问题,但看不出任何可行的答案,虽然它看起来是一个非常基本的需求,当我得知它不受支持时,这是一个惊喜。

Thanks! 谢谢!

EDIT: The problem is NOT parsing the XML, that I know how to do. 编辑:问题是没有解析XML,我知道该怎么做。 The problem is running XPath queries on the parsed XML. 问题是在解析的XML上运行XPath查询。

Right now the required support is for Android and iOS native browsers(that are both webkit-based), but Windows Phone might need support soon too. 目前所需的支持是Android和iOS原生浏览器(基于webkit),但Windows Phone可能也很快就需要支持。

You can get xml values like this: 你可以得到像这样的xml值:

$(myXML).find('person').each(function(i, val) {
  // i is the counter of this element
  var age = $(val).attr('age'); // this is an attribute of the person node
  var firstName = $(val).find('firstname').text();
  var lastName = $(val).find('lastname').text();
}

This will work with: 这将适用于:

...
<person age="3">
<firstname>Babe</firstname>
<lastname>Ruth</lastname>
</person>
<person age="44">
<firstname>Hank</firstname>
<lastname>AAron</lastname>
</person>
...

See here 看到这里

You can also do: 你也可以这样做:

$('person', myXML).each(function(){
    //blah
});

The selector takes two parameters. 选择器有两个参数。 The second parameter is the context in which to find the selector which defaults to document 第二个参数是查找默认为document的选择器的上下文

Well, I've found no good answer. 好吧,我找不到好的答案。

As a viable alternative I've found this nice library from GoogleCode: http://goog-ajaxslt.sourceforge.net/ 作为一种可行的替代方案,我从GoogleCode中找到了这个很好的库: http ://goog-ajaxslt.sourceforge.net/
There's a cross-browser XPath implementation there that can be used independently from the whole framework, and work's great. 那里有一个跨浏览器的XPath实现,可以独立于整个框架使用,并且工作得很好。

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

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