XPath : Find following siblings that don't follow an order pattern

标签 xpath axes

这是用于 C 代码检测。我正在尝试标记没有中断的 case 语句。当 break 语句之前有多行时,树的层次结构如下所示。这是 C 中的示例:

switch (x) {
  case 1:
    if (...) {...}
    int y = 0;
    for (...) {...}
    break;
  case 2:

它以某种方式表示为:

<switch>
  <case>...</case>
  <if>...</if>
  <expression>...</expression>
  <for>...</for>
  <break>...</break>
  <case>...</case>
</switch>

我需要找到 <case> <break> 在哪里存在于任意行之后,但在下一个 <case> 之前.

此代码仅帮助我找到那些中断不立即跟随大小写的代码:

//case [name(following-sibling::*[1]) != 'break']

..但是当我尝试使用 following-sibling::* 时,它会找到中断,但不一定在下一个案例之前。

我该怎么做?

最佳答案

选择任何 case 有一个 break 没有后续 case 或下一个 break 小于下一个 case 的位置。通过对前面的 sibling 运行 count() 来确定位置。

//case
[
    following-sibling::break and
    (
        not(following-sibling::case) or
        (
            count(following-sibling::break[1]/preceding-sibling::*) <
            count(following-sibling::case[1]/preceding-sibling::*)
        )
    )
]

要获取其他情况,那些没有中断的情况,只需像这样在其中扔一个大的旧not():

//case
[not(
    following-sibling::break and
    (
        not(following-sibling::case) or
        (
            count(following-sibling::break[1]/preceding-sibling::*) <
            count(following-sibling::case[1]/preceding-sibling::*)
        )
    )
)]

关于XPath : Find following siblings that don't follow an order pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613997/

相关文章:

.net - 当属性已声明为父级的属性时,使用 XPath 匹配名称格式为 ParentElement.Property 的元素

layout - 如何在 Plotly 子图中添加第二条轨迹的轴?

python - Mayavi,python axes INDICATOR

javascript - Highcharts:使 X 轴和 Y 轴相交于 0

java - JFreeChart 域/范围轴定义

python - 如何在Python中的固定图形内更改轴大小?

Java : Parsing xml file using SAX/XPATH

java - 如何使用JAVA实现XQL Join?

string - 在XPATH中获取属性的子字符串

python - 如何使用 selenium python 绑定(bind)的相对 xpath 来定位 dom 元素?