简体   繁体   English

使用XPath,如何在没有前置文本的情况下获得标记的完全匹配?

[英]Using XPath, how can I get an exact match at tag without preceding text?

The doubt is with xpath in the given scenario: 疑问是在给定场景中使用xpath:
I have this two dropdown item inside a customized dropdown component, this component has a textbox to search items that contains the informed text. 我在自定义下拉组件中有这两个下拉项,此组件有一个文本框,用于搜索包含通知文本的项目。 Using XPath expression, I want to get the dropdown-item that exact match the informed text which I put at the textbox and not returns the dropdown-item that contains a text before the span tag. 使用XPath表达式,我想获得与我放在文本框中的通知文本完全匹配的下拉项,而不是返回包含span标记之前的文本的下拉项。 This span surround the item that match the informed criteria. 此跨度围绕符合通知标准的项目。

To be clearer, let's look at the html code itself where we have this issue: 为了更清楚,让我们看看我们遇到此问题的html代码本身:

This first dropdown-item, contains the item that I want to return. 第一个下拉项目,包含我想要返回的项目。 The span that contains text Steak and doesn't contains any above text, inside the div. 包含文本Steak的跨度,并且不包含div中的任何上述文本。

<div id="dropdown-item" role="option">
<span class="select2-match">Steak</span>
 - Delicious Salmon Steak
</div>

This second item, contains the item that the xpath expression should ignore, because it contains a text above the span node, inside the dropdown-item div. 第二个项目包含xpath表达式应忽略的项目,因为它在dropdown-item div中包含span节点上方的文本。

<div id="dropdown-item" role="option">
Double
<span class="select2-match">Steak</span>
 - Monster Fillet Steak
</div>

I tried a lot of XPath expressions and I'm starting to think that only with XPath I won't be able to select the item I want. 我尝试了很多XPath表达式,我开始认为只有XPath我才能选择我想要的项目。 I know that if I concatenate two or more XPaths in programming (C# or Java, for example) and give a substring by the hyphen and in the end find the index that I need, I'll be able to find the required node, but with the amount of information I'll be working, it can not be performatic. 我知道如果我在编程中连接两个或多个XPath(例如C#或Java)并通过连字符给出一个子字符串,最后找到我需要的索引,我将能够找到所需的节点,但是随着我将要工作的信息量,它不能是性能。 The idea would be to use only xpath, perhaps with regex... Any suggestion? 想法是只使用xpath,也许使用正则表达式...任何建议?

This is one possible XPath (formatted for readability) : 这是一个可能的XPath(为便于阅读而格式化):

//div[@id='dropdown-item']
/span[.='Steak']
     [not(
            preceding-sibling::text()[normalize-space()]
       )
     ]

brief explanation : 简要说明 :

  • //div[@id='dropdown-item'] : find all div element, anywhere in the HTML document, having id attribute value equals "dropdown-item" //div[@id='dropdown-item'] :在HTML文档中的任何位置找到所有div元素, id属性值等于"dropdown-item"
  • /span[.='Steak'] : from each of such div , find child element span , having inner text exactly equals "Steak" .... /span[.='Steak'] :从每个这样的div ,找到子元素span ,内部文本正好等于"Steak" ....
  • [not(preceding-sibling::text()[normalize-space()])] : ... and not having a non-empty preceding-sibling text node. [not(preceding-sibling::text()[normalize-space()])] :...并且没有非空的前兄弟文本节点。 This will match span having white-space only preceding sibling text, or not having any preceding sibling text at all. 这将匹配仅在兄弟文本之前具有空白空间的span ,或者根本没有任何先前的兄弟文本。

我会让你调整XPath以满足你的需求,但这是一个开始:

//div[span[normalize-space(preceding-sibling::text())='']]

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

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