草庐IT

xml - XPath 查询以选择没有特定属性的特定值的任何后代

coder 2024-06-28 原文

我一直在尝试构建一个基本上选择所有内容但排除某些节点的 XPath 查询。

这是我正在处理的 XML:

<?xml version="1.0" encoding="UTF-8"?>

<task>
  <title id="30014">Instructions</title>
  <taskbody>
    <context>
      <p>Your box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>
    <steps audience="print">
      <step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>
      <step>
        <cmd>Click on the “Resource” button.</cmd>
        <info>
          <fig frame="all">
            <image href="resource.ai" height="1.650in" width="4.500in"/>
          </fig>
        </info>
      </step>
      <step>
        <cmd>Click on “Manuals”.</cmd>
      </step>
      <step>
        <cmd>Click on “Shipping”.</cmd>
      </step>
      <step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>
    </steps>
  </taskbody>
</task>

我需要选择观众不等于“打印”的所有内容。

我一直在尝试我阅读过的各种方法,但似乎没有一个能完全按照我需要的方式工作。

这是最新的一个接近但不完全的:

task/taskbody//*[not(@audience = "print")]

问题是,它可以很好地去除具有“打印”值的下一级节点。然而,第一个 <p>具有“打印”值的在 <context> 内.该节点似乎永远不会被选中。

查询结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<result>
<context>
      <p>Your box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>

<p>Your box has a document.</p>

<p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>

<xref href="/node/6308" scope="external">Click here</xref>

<step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>

<cmd>Go to 
          <u>www.google.com</u>.
        </cmd>

<u>www.google.com</u>

<step>
        <cmd>Click on the “Resource” button.</cmd>
        <info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>
      </step>

<cmd>Click on the “Resource” button.</cmd>

<info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>

<fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>

<image height="1.650in" href="resource.ai" width="4.500in"/>

<step>
        <cmd>Click on “Manuals”.</cmd>
      </step>

<cmd>Click on “Manuals”.</cmd>

<step>
        <cmd>Click on “Shipping”.</cmd>
      </step>

<cmd>Click on “Shipping”.</cmd>

<step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>

<cmd>You can save or print it from your browser window.</cmd>

</result>

它抓取没有属性的节点,它抓取带有“web”的节点以及除了那个以外的大多数带有“print”的节点。

有什么建议吗?

最佳答案

这个表达式将选择所有没有任何 @audience 的元素属性,以及那些包含一个不是字符串 print 的值的属性:

//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]

上面的写法会选择<title> , 第一和第三 <p> <context> 的 children .它不会选择 <steps>或第二个<p>因为他们有一个 audience包含 print 的属性.

要排除标题(将上下文缩减为 taskbody),请使用:

//task/taskbody//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])] 

关于xml - XPath 查询以选择没有特定属性的特定值的任何后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24398343/

有关xml - XPath 查询以选择没有特定属性的特定值的任何后代的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用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

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  4. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  5. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  6. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  7. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  8. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  9. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  10. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

随机推荐