Xml XPath查询以选择没有特定属性的特定值的任何子体
我试图构造一个XPath查询,它基本上选择所有内容,但排除某些节点 这是我要介绍的XML:Xml XPath查询以选择没有特定属性的特定值的任何子体,xml,xpath,Xml,Xpath,我试图构造一个XPath查询,它基本上选择所有内容,但排除某些节点 这是我要介绍的XML: <?xml version="1.0" encoding="UTF-8"?> <task> <title id="30014">Instructions</title> <taskbody> <context> <p>Your box has a document.</p>
<?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>
问题是,它在剥离具有“print”值的节点1层时效果很好。但是,具有“print”值的第一个
位于
的内部。该节点似乎永远不会被选中
以下是查询结果:
<?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>
你的箱子里有一个文件
要获取文档,请执行以下操作:
单击此处获取文档。
你的箱子里有一个文件
单击此处获取文档。
点击这里
去
www.google.com。
去
www.google.com。
www.google.com
点击“资源”按钮。
点击“资源”按钮。
点击“手册”。
点击“手册”。
点击“发货”。
点击“发货”。
您可以从浏览器窗口保存或打印它。
您可以从浏览器窗口保存或打印它。
它抓取没有属性的节点,它抓取带有“web”的节点,以及除该节点外带有“print”的大多数节点
有什么建议吗?此表达式将选择所有不具有任何
@viewer
属性的元素,以及那些具有属性但包含非字符串print
值的元素:
//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]
按照上面的书写方式,它将选择
、第一个和第三个
子级
。它不会选择
或第二个
,因为它们有一个访问者
属性,包含打印
要排除标题(将上下文缩减为任务体
),请使用:
我尝试了这个查询,结果是:
说明您的框中有一个文档。单击此处获取文档
单击此处
。看起来好像脱得太多了。这不是你想要的吗?所有没有值为print
的acquisition
属性的节点?等等,对不起,我想你是对的。(我今天已经盯着这个XML看了太久了。那么你还有什么其他限制?好吧,看来唯一的问题是结果包含了两次节点中的一个。结果中的
显示在
的上下文中,并且它本身。因此理想情况下,结果将包含以下内容:指令你的框中有一个文档。单击此处获取文档。
(同上,但没有第二个
实例)。疑问:当您试图使用audience=“print”排除元素时
是否也应排除整个..
节点,包括
子节点?您是否期望?
//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]
//task/taskbody//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]