Stefan Gentz
Stefan Gentz

Reputation: 419

Selecting nodes between two processing instructions with XPATH

I have something like this:

<?cond_start condition="Online" ?>
<p>This section is tagged with Conditional "Online".</p>
<?cond_end?>

<?cond_start condition="Print" ?>
<p>This section is tagged with Conditional "Print".</p>
<?cond_end?>

I need to process the content between the PIs based on the value of the PIs (condition="Online" / condition="Print"). I can select a specific PI with e.g. this:

//processing-instruction('cond_start')

But I have no idea how to go beyond this … Especially not, as there can be nested PIs like this:

<?cond_start condition="Online" ?>
<p>This section is <?cond_start condition="Comment" ?>Are you sure?<?cond_end?> tagged with Conditional "Online".</p>
<?cond_end?>

Aynone any ideas?

Upvotes: 0

Views: 419

Answers (1)

Tomalak
Tomalak

Reputation: 338128

I think you want all nodes where

  • the first preceding processing instruction node is itself a cond_start which has a certain contents, for example 'condition="Online"'.
  • the first following processing instruction node is itself a cond_end

That would be:

//node()[
    preceding-sibling::processing-instruction()[1][
        self::processing-instruction('cond_start')
        and contains(., 'condition="Online"')
    ]
    and following-sibling::processing-instruction()[1][
        self::processing-instruction('cond_end')
    ]
]

Note that this does not work when there are more processing instructions between <?cond_start condition="..." ?> and <?cond_end?>. If that's the case for you things get more complicated.

Upvotes: 1

Related Questions