How can I use recursive AND conditional selection in XPath?
For example, given this document:
<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>
I would like to select just:
<file>1</file>
<file>2</file>
<file>3</file>
<file>4</file>
So, effectively this:
//[name="foo.mp4"]/chunks/*[1]/chunks/*[1]/*
But with a generalized approach -- i.e something that would cover even deeper-nested objects. Something like this:
//[name="foo.mp4"]/(chunks/*[1]/)+/*
(cond)+
is not XPath syntax, and a regex-like representation of what I want.
Recursion implies self-reference and is not directly available in XPath. The usual way to ignore intervening levels of elements is via the
descendant-or-self
axis (//
), anchored by a desired property.For example, each of the following XPath expressions,
All
file
elements with values less than 5:The first 4 leaf
file
elements:The
file
leaf elements whose ancestors have no predecessors:will select
as requested.