递归 XPath 的条件

jbe*_*net 3 xml xpath recursive-regex

如何在 XPath 中使用递归 AND 条件选择?

例如,给定此文档:

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
  <file name="foo.mp4">
    <chunks>
      <file>
        <chunks>
          <file>
          <chunks>
            <file>1</file>
            <file>2</file>
            <file>3</file>
            <file>4</file>
          </chunks>
          </file>
          <file>
          <chunks>
            <file>5</file>
            <file>6</file>
            <file>7</file>
            <file>8</file>
          </chunks>
          </file>
        </chunks>
      </file>
      <file>
        <chunks>
          <file>
          <chunks>
            <file>9</file>
            <file>10</file>
            <file>11</file>
            <file>12</file>
          </chunks>
          </file>
          <file>
          <chunks>
            <file>13</file>
            <file>14</file>
            <file>15</file>
            <file>16</file>
          </chunks>
          </file>
        </chunks>
      </file>
    </chunks>
  </file>
</root>
Run Code Online (Sandbox Code Playgroud)

我只想选择:

<file>1</file>
<file>2</file>
<file>3</file>
<file>4</file>
Run Code Online (Sandbox Code Playgroud)

所以,实际上是这样的:

//[name="foo.mp4"]/chunks/*[1]/chunks/*[1]/*
Run Code Online (Sandbox Code Playgroud)

但采用通用方法——即可以覆盖更深嵌套对象的方法。像这样的东西:

//[name="foo.mp4"]/(chunks/*[1]/)+/*
Run Code Online (Sandbox Code Playgroud)

(cond)+不是 XPath 语法,而是我想要的类似正则表达式的表示。

kjh*_*hes 5

递归意味着自引用,并且在 XPath 中不能直接使用。忽略元素的干预级别的常用方法是通过descendant-or-self轴 ( //),由所需的属性锚定。

例如,以下每个 XPath 表达式,

将选择

<file>1</file>
<file>2</file>
<file>3</file>
<file>4</file>
Run Code Online (Sandbox Code Playgroud)

按照要求。