0
votes

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?

1
What exactly should be selected? All the p elements that are between cond_start and cond_end PIs? And, more importantly, are you sure this is a representative sample of your input XML? Include p elements that should not be selected, for example, because as the input is now, a simple //p would work. - Mathias Müller
Sorry for not being precise enough ... I need to process everything between <?cond_start condition="foo" ?> and <?cond_end?>. This can be p or anything else. (Actually it's XHTML with PIs for conditional output. PIs don't have child nodes, so I need to find a way to select everything between two specific PIs. - Stefan Gentz

1 Answers

1
votes

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.