我如何从以下文档中找到具有最大版本的节点:
<GateKeeperFiles>
<File>
<Name>GateKeeper.exe</Name>
<Major>2</Major>
<Minor>1</Minor>
<Build>1</Build>
<Revision>6</Revision>
</File>
<File>
<Name>GateKeeper.exe</Name>
<Major>1</Major>
<Minor>1</Minor>
<Build>1</Build>
<Revision>9</Revision>
</File>
</GateKeeperFiles>
理想情况下,这可以通过单个 XPath 函数实现。我目前有两个函数可以让我获得最大的主要值(value),但我似乎无法从那里取得进展。
/GateKeeperFiles/File[not (Major <= preceding-sibling::File/Major) and not(Major <= following-sibling::File/Major)]
或
/GateKeeperFiles/File[not(/GateKeeperFiles/File/Major > Major)]
干杯, 史蒂夫
最佳答案
如果您使用的是 C#,它必须是 xpath 吗?例如(编辑以支持具有相同版本的多个文件 - 帖子提到复数):
XDocument doc = XDocument.Parse(xml);
var nodes =
from file in doc.Document
.Element("GateKeeperFiles")
.Elements("File")
select new {
Node = file,
Version = new Version(
(int) file.Element("Major"),
(int) file.Element("Minor"),
(int) file.Element("Build"),
(int) file.Element("Revision"))
} into tmp
orderby tmp.Version descending
select tmp;
var mostRecentVersion = nodes.Select(x => x.Version).FirstOrDefault();
var files = nodes.TakeWhile(x => x.Version == mostRecentVersion);
foreach(var file in files) {
Console.WriteLine("{0}: {1}",
file.Version,
(string)file.Node.Element("Name"));
}
或使用 2.0(来自 OP 评论):
static int GetVersion(XmlNode element, string xpath) {
return int.Parse(element.SelectSingleNode(xpath).InnerText);
}
static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Version bestVersion = null;
List<XmlElement> files = new List<XmlElement>();
foreach (XmlElement file in doc.SelectNodes(
"/GateKeeperFiles/File")) {
Version version = new Version(
GetVersion(file, "Major"), GetVersion(file, "Minor"),
GetVersion(file, "Build"), GetVersion(file, "Revision"));
if (bestVersion == null || version > bestVersion) {
bestVersion = version;
files.Clear();
files.Add(file);
} else if (version == bestVersion) {
files.Add(file);
}
}
Console.WriteLine("Version: " + bestVersion);
foreach (XmlElement file in files) {
Console.WriteLine(file.SelectSingleNode("Name").InnerText);
}
}
关于c# - XPath 版本搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/515256/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题: