我正在阅读有关快捷方式“//”的信息,它显然是用于:
'/后裔或自己'
从这样一个表达式的简单示例中可以清楚地知道会发生什么, 例如,
//我的节点
它将返回文档中所有实例的节点列表,从根节点找到名为“myNode”的元素。
但是,更复杂的表达是什么意思,比如:
//一个节点//我的节点
?
因为//(作为'/descendant-or-self'的快捷方式)匹配根节点两次, 这是否意味着表达式“//aNode”的第一部分是多余的,只会增加完成表达式执行所需的时间(在仍然只找到整个文档中的所有表达式之后,“myNode”) ?
“//myNode”和“//aNode//myNode”会产生完全相同的结果吗?
最后,如果我在文档中搜索节点“myNode”的实例,它是节点“interestingTree”的间接后代。但我不想要节点“myNode”的实例,它是节点“nonInterestingTree”的间接后代, 我该怎么做?
例如在文档中搜索:
<root>
<anode>
<interestingTree>
<unknownTree>
<myNode/><!-- I want to find this one, not the other, where I don't know the path indicated by 'unknownTree' -->
</unknownTree>
</interestingTree>
<nonInterestingTree>
<unknownTree>
<myNode/>
</unknownTree>
</nonInterestingTree>
</anode>
<anode>
<someOtherNode/>
</anode>
</root>
谢谢!
最佳答案
Are '//myNode' and '//aNode//myNode' going to result in exactly the same thing?
是的,在这种情况下,因为所有 myNodes 也是 anode 的后代。然而,在一般意义上,//aNode//myNode 显然不会匹配其祖先树中没有 anode 父节点的节点。
xpath:
//aNode//myNode
将忽略 aNode 和 myNode 之间的任何中间层次结构,即它将匹配 /aNode/myNode、/anyNodes/anode/myNode 和 /anyNodes/anode/xyzNode/myNode
这回答了你的最后一个问题,你可以像这样在有趣的子路径中找到节点:(同样,忽略层次结构中的任何中间元素)
//anode//interestingTree//myNode
当然,理想情况下,您的路径应该尽可能明确,因为 // 可能需要搜索大量元素,从而导致性能开销。
编辑这可能有帮助吗?
为了清晰起见,我已将您的 xml 输入调整为:
<root>
<anode>
<interestingTree>
<unknownTree>
<myNode>
MyNode In Interesting Tree
</myNode>
</unknownTree>
</interestingTree>
<nonInterestingTree>
<unknownTree>
<myNode>
MyNode In Non-Interesting Tree
</myNode>
</unknownTree>
</nonInterestingTree>
</anode>
<anode>
<someOtherNode/>
</anode>
<bnode>
<myNode>
MyNode in BNode
</myNode>
</bnode>
</root>
通过样式表解析时:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
Matched by `//myNode`
<xsl:apply-templates select="//myNode">
</xsl:apply-templates>
Matched by `//aNode//myNode`
<xsl:apply-templates select="//anode//myNode">
</xsl:apply-templates>
Matched by `//aNode//interestingTree//myNode`
<xsl:apply-templates select="//anode//interestingTree//myNode">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="myNode">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
返回以下内容:
Matched by `//myNode`
MyNode In Interesting Tree
MyNode In Non-Interesting Tree
MyNode in BNode
Matched by `//aNode//myNode`
MyNode In Interesting Tree
MyNode In Non-Interesting Tree
Matched by `//aNode//interestingTree//myNode`
MyNode In Interesting Tree
关于xml - xpath/descendant-or-self - 在特定树中搜索节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12530657/
我正在学习如何使用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
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我使用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
我正在学习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)时忽略某些行的最佳方法是什么?我正在尝试仅解析Cucumber.feature文件中的场景,并希望跳过不以Scenario/Given/When/Then/And/But开头的行。下面的代码有效,但它很荒谬,所以我正在寻找一个聪明的解决方案:)File.open(file).each_linedo|line|line.chomp!nextifline.empty?nextifline.include?"#"nextifline.include?"Feature"nextifline.include?"Inorder"nextifline.include?
我在搜索我的值是方法的散列时遇到问题。我只是不想运行plan_type与键匹配的方法。defmethod(plan_type,plan,user){foo:plan_is_foo(plan,user),bar:plan_is_bar(plan,user),waa:plan_is_waa(plan,user),har:plan_is_har(user)}[plan_type]end目前如果我传入“bar”作为plan_type,所有方法都会运行,我怎么能只运行plan_is_bar方法呢? 最佳答案 这个变体怎么样?defmethod
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
#app/models/product.rbclassProduct我从Controller调用方法1。当我运行程序时。我收到一个错误:method_missing(atlinemethod2(param2)).rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/relation/batches.rb:59:in`block(2levels)infind_each... 最佳答案 classProduct说明:第一个是类
我知道还有其他相同的问题,但他们没有解决我的问题。我不断收到错误:Aws::Errors::MissingRegionErrorinBooksController#create,缺少区域;使用:region选项或将区域名称导出到ENV['AWS_REGION']。但是,这是我的配置开发.rb:config.paperclip_defaults={storage::s3,s3_host_name:"s3-us-west-2.amazonaws.com",s3_credentials:{bucket:ENV['AWS_BUCKET'],access_key_id:ENV['AWS_ACCE
我明白了defa(&block)block.call(self)end和defa()yieldselfend导致相同的结果,如果我假设有这样一个blocka{}。我的问题是-因为我偶然发现了一些这样的代码,它是否有任何区别或者是否有任何优势(如果我不使用变量/引用block):defa(&block)yieldselfend这是一个我不理解&block用法的具体案例:defrule(code,name,&block)@rules=[]if@rules.nil?@rules 最佳答案 我能想到的唯一优点就是自省(introspecti